Charlie Davis
Charlie Davis

Reputation: 27

Apache rewrite rule not triggering

I want to take this URL:

http://www.awesomehost.com/accountants/Ceribelli-Associates-cd01qazwsxoztfnlvsim.html

And redirect it to:

http://www.awesomehost.com/accountants/accountant-directory.php?ID=cd01qazwsxoztfnlvsim

And here's what I have in my .htaccess for the accountants directory:

RewriteEngine On
RewriteRule ^accountants/(.*)-cd0(.*).html /accountants/accountant-directory.php?ID=cd0$2 [L,P]

I have also tried it with a leading slash and the P flag removed:

RewriteEngine On
RewriteRule ^/accountants/(.*)-cd0(.*).html /accountants/accountant-directory.php?ID=cd0$2 [L]

But for some reason, its not being triggered?

When I put in some random text in the .htaccess file, I get apache errors so I know its reading the file...

Any help out there??

Upvotes: 0

Views: 110

Answers (2)

AlexanderZ
AlexanderZ

Reputation: 521

Try removing the "accountants/" part of the pattern. Apache only starts matching from the current directory of the .htaccess file.

RewriteEngine On
RewriteBase /accountants
RewriteRule ^(.*?)-cd0(.*?).html accountant-directory.php?ID=cd0$2 [L]

Upvotes: 0

Conrad Shultz
Conrad Shultz

Reputation: 8808

RewriteRule begins a match after the TLD, and you are matching start of line without a leading slash. So what if you try:

RewriteRule ^/accountants/(.*)-cd0(.*).html ...

Btw, be careful using a programatically generated URL with a .* rewrite - watch out for greedy vs. non-greedy matching.

Also, be sure that [P] is what you want? You could run the risk of proxying to unintended servers if a maliciously crafted URL were used and your RewriteRule is inadequately filtered.

Upvotes: 1

Related Questions