homepage Welcome to WebmasterWorld Guest from 54.242.231.109
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
Pluspad




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

Hi,

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]

RewriteCond %{ENV:REDIRECT_STATUS} ^$

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.

Thanks
Alan

 

lucy24




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

Oy.

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
RewriteCond %{ENV:REDIRECT_STATUS} ^$
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 ;)

Pluspad




msg:4546782
 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.
Thanks
Alan

lucy24




msg:4546930
 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")
and
$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
([mv]/)

Pluspad




msg:4547491
 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...

Thanks
Alan

g1smd




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

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

$1 substitutes it back into the new path.
Pluspad




msg:4547830
 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

Alan

g1smd




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

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

lucy24




msg:4548063
 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