2

I am very well aware that such type questions have already been asked many times but I think mine is a different one. I am a newbee to .htaccess.

RewriteEngine On 
RewriteBase /

RewriteCond %{HTTP_HOST} ^localhost/index.php
RewriteRule (.*) localhost/index.php [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ leather-product.php?n=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^bagsgentssel1/([0-9]+)$ bagsgentssel1.php?h=$1 [L] 

# RewriteRule . /index.php [L]
# RewriteRule ^bagsgentssel1/([0-9]+)$ bagsgentssel1.php?h=$1 [QSA,L]
# RewriteRule ^(.*)$ leather-product.php?n=$1 [QSA,L]

When I run my code RewriteRule ^(.*)$ leather-product.php?n=$1 [QSA,L], this works fine. But when I try to run the second rule RewriteRule ^bagsgentssel1/([0-9]+)$ bagsgentssel1.php?h=$1 [L], this does not work. No error is shown here and in the chrome browser URL is shown as http://localhost/bagsgentssel1/45.

Interesting part is if I comment below code then the second RewriteRule ^bagsgentssel1/([0-9]+)$ bagsgentssel1.php?h=$1 [L] works fine.

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ leather-product.php?n=$1 [QSA,L]

I am unable to find where I've gone wrong?

0

You have the directives in the wrong order. You need the more specific directives first.

RewriteRule ^(.*)$ leather-product.php?n=$1 [QSA,L]

Your first rule catches everything (with pattern ^(.*)$), so your second rule is never actually reached. You need to either make the above RewriteRule pattern more specific (eg. only match numbers or something) so that it doesn't conflict with the second rule. Or reverse your two rules to have the second (more specific) rule first:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^bagsgentssel1/([0-9]+)$ bagsgentssel1.php?h=$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ leather-product.php?n=$1 [QSA,L]

Interesting part is if I comment below code then the second RewriteRule ^bagsgentssel1/([0-9]+)$ bagsgentssel1.php?h=$1 [L] works fine.

Exactly, you are removing the directive that conflicts / catches everything.

mod_rewrite directives are processed top to bottom. If a directive at the top of the file catches the request then all directives that follow could be skipped.

If you have no other directives then the above could be rewritten:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^bagsgentssel1/([0-9]+)$ bagsgentssel1.php?h=$1 [L] 

RewriteRule ^(.*)$ leather-product.php?n=$1 [QSA,L]

Aside:

RewriteCond %{HTTP_HOST} ^localhost/index.php
RewriteRule (.*) localhost/index.php [R=301,L]

I'm not sure what this is supposed to be doing, but it's not actually doing anything (the condition will never match). Although if it did, it looks like it would break.

  • Thank you very much for your kind help. Can you please suggest any book or reference guide from where i can learn more about RewriteRule/RewriteCond used in htaccess. – Suneet Feb 7 at 6:03
  • If this answered your question then please mark it as accepted (checkmark below the voting arrows on the left) to remove it from the unanswered question queue. Once you have 15+ rep then you can also upvote answers you find useful. Thanks, much appreciated. :) – MrWhite Feb 7 at 11:26
  • Although not everyones cup of tea, my go-to is the Apache Docs: Apache mod_rewrite Introduction and Apache Module mod_rewrite - very concise, but perhaps lacking in "depth". When undertsanding mod_rewrite you need to question every single character, unfortunately mod_rewrite directives are notoriously copy/pasted and missinformation spreads. – MrWhite Feb 7 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.