Forum Moderators: phranque

Message Too Old, No Replies

Having problems with mod rewrite

I need a SEO friendly profile page

         

bmarshall0511

8:24 pm on Dec 14, 2009 (gmt 0)

10+ Year Member



Alright so I'm new to mod_rewrite so bare with me. Basically I have a file:

'profile.php?id=16&u=username'

I need it to be in a friendly SEO format like this:
'16/username'

This is what I have in my .htaccess currently:

RewriteBase /
RewriteRule ^([^/]*)/([^/]*)$ /profile.php?id=$1&u=$2 [L]

It works BUT, I've got some subdomains on that site and when I go to them I get an Internal Server Error.

Any help will be greatly appreciated.

jdMorgan

11:05 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suspect you mean that you've got some subdomains mapped into the same filespace as this site, and located in subdirectories below the directory in which this .htaccess file resides.

If that's the case, then the problem is likely that your rule has no way to differentiate requests for the URL-path "16/username" which it should rewrite to your script from requests for the URL-path "subdomain-name/some-file.ext" which it should not rewrite to your script.

The root cause of the problem is that your subdomain subdirectories aren't 'separated' from the main domain's other subdirectories in any appreciable way, other than by their individual names. In other words, it would have been better to have
/root
. /maindomain
. /subdomain1
. /subdomain2
. /subdomain3

So that your main domain's and your subdomains' files weren't "all mixed up" with each other in and under root.

So that leaves you with three choices:

1) Exclude each subdomain subdirectory from your rules using a negative-pattern RewriteCond like

RewriteCond %{REQUEST_URI} !^/subdomain1/ 

2) Change your 'friendly URLs' and your RewriteRule so that your 'user' rewrites can be identified easily. For example, use a friendly URL like "/users/16/username" or "/users/username-16", etc.

3) Change your RewriteRule pattern to require numbers and only numbers in the first path-part. i.e. change the first "([^/]+)" to "([0-9]+)". This may sound like the most appealing solution to you right now, as it requires less work, but it is also the most likely to cause problems in the future -- say if you install a blog or forum or something that has numeric characters in the initial path-part, and insufficient flexibility to adapt to and co-exist with your currently-existing URL- and file-structure.

So anyway, the idea here is to organize your URLs and files in the most regular, distinctive, and unambiguous way possible -- always. This will prevent major headaches in the future when you install scripts that have less-than-total flexibility, by avoiding naming "collisions."

Basically, if you've got multiple "things" in your filespace, then those "things" should be in subdirectories of a directory named "things" so they can be kept apart from other "gizmos" which might need to have the same filename and which should be stored in subdirectories of a directory named "gizmos". This will prevent potentially-big problems in the future, and make it much easier to write simple, efficient RewriteRules -- both now and later.

Jim

bmarshall0511

12:15 am on Dec 15, 2009 (gmt 0)

10+ Year Member



I really appreciate the information. It helps a lot. I'm going to create a main sub-domain like you suggested and assuming (again, very unfamiliar with mod_rewrite and .htaccess in general) that each sub-domain will have an independent .htaccess file it will use versus right now it using the root's for all the sub-directories?

Thanks again, I've literally been working on this since 9am this morning (it's 6pm now).