Help with URL Rewrite rule

I’ve been trying to figure this out, but can’t seem to get it to work. If anyone could help out by letting me know if my rule is correct or not, I can maybe figure out what the issue might be if its somewhere else.

Its a pretty standard rewrite, i.e. to change:

www.mydomain.com/subfolder/index.php?LodgeID=123

to

www.mydomain/subfolder/123/

I thought it should be something like:

RewriteRule ^/lodge/([0-9]+)$ /index.php?LodgeID=$1

Bu that doesn’t seem to do anything - am I barking up the right tree?

Thanks.

I assume you do have

RewriteEngine On

somewhere in your code?

Other than that, you must remove the first slash, and add a slash at the end, like so


RewriteRule ^lodge/([0-9]+)/$ /index.php?LodgeID=$1 [L]

If also added a [L] flag, to make sure that if the rule matches mod_rewrite will stop the current round of rewriting.

JG,

RewriteEngine on simply ensures that the mod_rewrite engine is not in the comment mode, i.e., required only in unusual circumstances. Rémon is spot-on removing the leading / (in the DocumentRoot with this mod_rewrite code) and Last flag but I take exception to changing the directory level TWO times with that trailing / of his (it will impact the directory level of your relative links).

It’s a very minor point that the leading / in the redirection makes Apache look first to the SERVER’s root THEN to the domain’s DocumentRoot (where you have this code - so you don’t need that /). If an index.php exists in the server’s root, it will receive the redirection. Of course, any other filename COULD result in a security issue.

With those explanations, I would recommend:

RewriteEngine on
RewriteRule ^subfolder/([0-9]+)$ index.php?LodgeID=$1 [L]

Note that your “subfolder” need not exist (and probably shouldn’t) as it’s only being used as a “marker” to activate the mod_rewrite redirection.

Regards,

DK