PHP mod_rewrite

Associate
Joined
16 Aug 2004
Posts
268
I am trying to make my urls seo friendly ie:

www.domain.com/about-us

rather than

www.domain.com/index.php?seo_link=2

using the following in my .htaccess file which works fine

Code:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?seo_link=$1 [L]

The problem is when I try to access the admin section on www.domain.com/admin

instead of going to the admin folder it thinks im trying to load a page called admin.

Is there anything I can add to my .htaccess file to make this work?
 
Last edited:
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?seo_link=$1 [L]
 
The two RewriteCond lines are just stopping it from evaluating the following rule if the request URI points directly to a file or directory.
 
Instead of starting my own thread, I am trying to do something similar.

When the user goes to:

Code:
Http://www.mydomain.com/myweb/newpage

I want it to go redirect to:

Code:
Http://www.mydomain.com/myweb/index.php?p=newpage

Here was my attempt at a .htaccess file which I placed in the myweb folder:

Code:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

...didn't work - what am I doing wrong?

EDIT:

Before you ask - mod_rewrite is loaded, I checked.
 
Last edited:
I'm an idiot...lol...but it still doesn't work?

This is the new .htaccess:

Code:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?p=$1 [L]
 
Last edited:
Just been reading some more on mod_rewrite and it says stuff about reloading apache...?

Does that have to be done?...cause I am on shared hosting so thats not possible methinks.
 
Did you add these lines as suggested above?

Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
Ok now it works, have the following:

Code:
RewriteEngine on
ErrorDocument 404 /new/e404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/([^/\.]+)/?$ /new/index.php?p=$1 [L]

...but it doesn't display the page properly....no CSS...no Images, just the plain HTML text, I could have sworn its supposed to just mask the url...non?
 
Sounds like it's getting stuck in a redirect loop while trying to load the resources. No idea why that should be happening only when a trailing slash is added though :confused:

Out of interest, what happens if you remove the /? from the rule pattern?
 
How do I go about setting that rule as the last redirect rule, because I want to have a rule which breaks the current rule (if that makes sense):

Code:
RewriteEngine on
ErrorDocument 404 /index.php?p=404
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cv/?$ /cv.pdf
RewriteRule ^([^/\.]+)/?$ /index.php?p=$1 [L]

Out of interest, what happens if you remove the /? from the rule pattern?

It doesn't pick up the parameter properly...so just redirects as if there is no parameter....still no css/images tho.
 
Oh I see why it's happening: with the trailing slash, the browser thinks you're one directory deeper than you actually are, and so it looks for the images and style sheets under /new/webd/ rather than /new/. The solution to this is to use absolute URIs.

Still not sure why it'd be taking ages to load though.

As for the CV rule, you'll want to put that above the rewrite conditions since a) it doesn't need them and b) conditions only apply to the immediately succeeding rule. Also, put an [L] flag on it, since no more rules need to be processed if it's matched.
 
Last edited:
Oh I see why it's happening: with the trailing slash, the browser thinks you're one directory deeper than you actually are, and so it looks for the images and style sheets under /new/webd/ rather than /new/. The solution to this is to use absolute URIs.

I assume you mean absolute URIs to the images & css right?

This is what I now have in my .htaccess and the CV is working as is the other rules...I just wanted to check there was nothing wrong "logically" with it...like the wrong order or something....OK to have the 404 redirect at the top?

Code:
RewriteEngine on
ErrorDocument 404 /index.php?p=404
RewriteRule ^cv/?$ /cv.pdf [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ /index.php?p=$1 [L]
 
I assume you mean absolute URIs to the images & css right?

Yep.

This is what I now have in my .htaccess and the CV is working as is the other rules...I just wanted to check there was nothing wrong "logically" with it...like the wrong order or something....OK to have the 404 redirect at the top?

Code:
RewriteEngine on
ErrorDocument 404 /index.php?p=404
RewriteRule ^cv/?$ /cv.pdf [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ /index.php?p=$1 [L]

Looks fine to me :)
 
Back
Top Bottom