Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mod_rewrite / .htaccess help

Status
Not open for further replies.

Foamcow

Programmer
Nov 14, 2002
6,092
GB
I am trying to do a redirect using .htaccess to make the visible URLs a bit neater using the following Rewriterules

Code:
RewriteRule ^branches/(.*)/contact$ branches/branchcontact.php?id=$1 [QSA,L]
RewriteRule ^branches/(.*)$ branches/branchpage.php?id=$1 [QSA,L]

As an example a request for /branches/london redirects to branchpage.php?id=london

This appears to works fine.

The problem comes when I need to do a further rewrite to a contact page.
/branches/london/contact should redirect to /branches/branchcontact.php?id=london

However, the first rule seems to be intercepting it.
I was under the impression that the [L] directive would prevent further rewriterules being applied, but this doesn't seem to be the case here.

Can someone help? What am I doing wrong?
I've tried swapping the rules around and other things, but the best I seem to be able to manage is to get the URL to rewrite to the contact page but it still loads the branch page.



<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Hi

Foamcow said:
I was under the impression that the [L] directive would prevent further rewriterules being applied, but this doesn't seem to be the case here.
Yep, that seems to no work as expected in some cases/versions. I also had misunderstandings with that.

Personally I would try to make the regular expression abit restrictive.
Code:
RewriteRule ^branches/([red][^/]+[/red])/contact$ branches/branchcontact.php?id=$1 [QSA,L]
RewriteRule ^branches/([red][^/]+[/red])$ branches/branchpage.php?id=$1 [QSA,L]

Feherke.
 
I've fixed it... I think.

I changed my rules to

Code:
RewriteRule ^branches/(.*)/contact/$ branches/branchcontact.php?id=$1 [QSA,L]
RewriteRule ^branches/(.*)/$ branches/branchpage.php?id=$1 [QSA,L]


Then made sure that I had the trailing slash on the links within the site.
This seems to have got around the problem and it works as expected.

At the moment requests that don't contain the trailing slash redirect to the index page. This has a list of branches so it's no huge problem. However it would be nice to catch these and send the to the right place, so if anyone has any ideas I'd love to hear them.

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Feherke
I'm not that hot with Regexp. What does your expression match?

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Hi

Not-slash one or more times.

[tt]^branches/([red][^/]+[/red])/contact$[/tt] matches "branches/[red]hello[/red]/contact"
[tt]^branches/([red][^/]+[/red])/contact$[/tt] does not match "branches/[red]hello/world[/red]/contact"
[tt]^branches/([red][^/]+[/red])/contact$[/tt] does not match "branches//contact"

So between the given endpoints must be one directory name, not more.

Feherke.
 
Ah ok.
Thanks, that's probably a good idea.

I have it working but only if the URL has a trailing slash.
Ideally I need to get rid of that need.

I have this at the moment
Code:
#branch subdirectory redirects
RewriteRule ^branches/index.php$ branches/index.php [QSA,L]
RewriteRule ^branches/([^/]+)/contact/$ branches/branchcontact.php?id=$1 [QSA,L]
RewriteRule ^branches/([^/]+)/$ branches/branchpage.php?id=$1 [QSA,L]

So
"branches/london/" goes to branchpage.php?bid=london
"branches/london/contact/" goes to branchcontact.php?id=london

However, if I make the trailing slash optional then I get the following result:
Code:
RewriteRule ^branches/index.php$ branches/index.php [QSA,L]
RewriteRule ^branches/([^/]+)/contact/$ branches/branchcontact.php?id=$1 [QSA,L]
RewriteRule ^branches/([^/]+)(/?)$ branches/branchpage.php?id=$1 [QSA,L]

"branches/london/" goes to branchpage.php?bid=london
"branches/london" goes to branchpage.php?bid=london
"branches/london/contact/" goes to branchpage.php?bid=london (wrong)

I just can't seem to make that slash optional and not have the branchpage.php rewriterule match





<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Hi

Just one thing. If you do not want to capture it, do not need to enclose the trailing slash between parenthesis ( () ).
Code:
RewriteRule ^branches/([^/]+)/?$ branches/branchpage.php?id=$1 [QSA,L]
Anyway, does not harm. But additional capturing may slow down the regular expression.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top