0

I would like to RewriteRule for a Specific query attribute

e.g.

https://website.com/shop/?req=cosmetics

to:

https://website.com/shop/cosmetics

I achieved that by doing:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shop/(.+)$ shop/?req=$1 [L,QSA]

My problem is that this will replace any query attribute...

What i'm looking for is to perform the RewriteRule explicit on '?req=' and let any other attr e.g. '?foo=' just to pass and execute.

I know that this is possible with RewriteCond %{QUERY_STRING} but i can't make it work...

Thanks

5
  • Your rule is rewriting /shop/<anything> to /shop/?req=<anything>. What change do you want in this rewrite behavior? Commented Feb 14, 2018 at 19:30
  • I would like to ignore the rewriting rule in any other case but '?req='. So if i send /shop/?foo=whatever it should just ignore it like i never had any rewriting rule. Commented Feb 15, 2018 at 7:36
  • so i assume that i need a condition something like this %{QUERY_STRING} ^?req= . Only then it should let the rewrite rule to happen! Commented Feb 15, 2018 at 7:49
  • 1
    Is this your complete .htaccess? As I said you current rule is is rewriting /shop/<anything> to /shop/?req=<anything>. If you enter in browser /shop/?foo=<anything> then current rule isn't doing anything. Commented Feb 15, 2018 at 7:55
  • 1
    Hey anubhava, it turns out that i had a bug going on in my code... I was not returning false after my ?foo=test request, the code was just keep going doing other staff so i got confused... Thanks for confirming that this peace of code is correct! Commented Feb 15, 2018 at 8:11

1 Answer 1

1

The solution is to change first rule and do something like this,otherwise server can't predict itself other query string except the existing one req :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shop/(.*)/(.*)$ /shop/?$1=$2 [L,QSA]

So, the Request https://website.com/shop/?req=cosmetics will be from https://website.com/shop/req/cosmetics request and .as you suggested , https://website.com/shop/?foo=cosmetics will be from https://website.com/shop/foo/cosmetics request

If you want to apply this code from shop directory .htaccess file :

   RewriteEngine On       
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_URI} !^/shop/merchant 
   RewriteRule ^(.*)/(.*)$ /?$1=$2 [L,QSA]

it will exclude merchant as well , Test it .

Sign up to request clarification or add additional context in comments.

2 Comments

My app lives in the same directory '/shop/' the process is happening internally by checking the query attributes '/shop/?req=cosmetics' is to fetch cosmetic products, but i would like when i have e.g. /shop/?merchant=123 to NOT do any rewriterute! So i want the RewriteRule to happen ONLY when i have /shop/?req=<something>
Hey Mohammed Elhag, thanks for checking in to this, it turns out that i had a bug in my code that got me confused! :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.