homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Rewrite Rule Problem

 11:36 am on Feb 19, 2013 (gmt 0)


I obviously have no idea what I am doing when it come to URL rewriting. I have the following in my htaccess file:

RewriteEngine On

RewriteRule ^v$ /v/ [R=301,L]
RewriteRule ^m$ /m/ [R=301,L]


RewriteRule ^(res/)?v/response\.php$ /v/%2? [R=301,L]
RewriteRule ^(res/)?m/response\.php$ /m/%2? [R=301,L]

RewriteRule ^v/([^/]+)/?$ /res/v/response.php?id=$1 [QSA,L]
RewriteRule ^m/([^/]+)/?$ /res/m/response.php?id=$1 [QSA,L]

RewriteRule ^(v/.*)$ /res/$1 [QSA,L]
RewriteRule ^(m/.*)$ /res/$1 [QSA,L]

when I go to my [mydomain.com...] al works fine. When I try [mydomain.com...] nothing happens.

If someone could point me in the right direction here I would really appreciate it.




 1:17 pm on Feb 19, 2013 (gmt 0)


Before anyone starts pointing, you're going to need to backtrack. Explain in English what the RewriteRules are intended to do. Then we can start getting the mess sorted out.

Why do they always come in pairs (v and m) when it's the identical rule each time?

You've made one very obvious mistake in misunderstanding how a RewriteCond works. It applies only to the immediately following rule. If the request doesn't potentially match the rule-- for example if the rule says /v/ and the request is for /m/ --the Condition will not even be evaluated. It's a good practice to put a blank line after every rule to help keep this point clear. You can attach multiple Conditions to a single Rule, but you can't attach multiple Rules to the same Condition.

What is
intended to do? I strongly suspect there's a better way of saying it.

Take your time. Our ranking Apache authority is currently busy watching YouTube ;)


 3:18 pm on Feb 19, 2013 (gmt 0)

Sorry about that lucy24, I'll try to be a bit more verbose.

The code I posted is the entirety of the rewrite rule I have in my htaccess file, which is in the root of my website.

I have a folder structure of mydomain.com/res/v/response.php

The rules above successfully rewrites mydomain.com/v/$variable to mydomain.com/res/v/response.php?id=$variable

I then create the folder structure mydomain.com/res/m/response.php

I then duplicated the various bits of the rewrite rule I thought would work so that mydomain.com/m/$variable rewrites to mydomain.com/res/m/response.php?id=$variable

Now As you mentioned, and as I can see now, that doesn't work. /v/$variable works, /m/$variable does not.

Basically I need mydomain.com/v/$variable and mydomain.com/m/$variable to both work.

Hope that helps.


 10:55 pm on Feb 19, 2013 (gmt 0)

If it's the identical code, why don't you replace each /v/ and /m/ pair with something containing

([mv]) on the left ("pattern")
$1 on the right ("target")


Make sure you get the right number of slashes in the right place. You might want to capture packages, like


 4:39 pm on Feb 21, 2013 (gmt 0)

Sorry for the slow reply.

It's probably not clear from my post as it's only one letter difference, but /v/ goes to the response.php file in the /v/ directory, and /m/ goes to the response.php file in the /m/ directory. They are not identical code, I can't simply match v or m and send it to the same place, they have to go to different places...



 6:24 pm on Feb 21, 2013 (gmt 0)

([mv]) captures the letter that was requested.

$1 substitutes it back into the new path.

 12:12 pm on Feb 22, 2013 (gmt 0)

Thanks all for the help, it was WAY simpler than I was expecting. I ended up with:

RewriteRule ^([mv])/(\w+)$ res/$1/response.php?id=$2



 12:43 pm on Feb 22, 2013 (gmt 0)

Add the [L] flag to that, and you're done.


 10:22 pm on Feb 22, 2013 (gmt 0)

And, ahem, a / at the front of the target.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved