2

I am using Magento 2.1.

I am trying to add a rewrite rule in .htaccess file for url's starting with index.php?route=. to be replaced with the query string.

RewriteCond %{QUERY_STRING} ^index.php?route=.+$ [NC] RewriteRule ^(.*)$ /%{QUERY_STRING}\? [R=301,L]

But this url rewrite rule is not getting reflected in my site.

Am I editing the right file(root/.htaccess).?

Please suggest a solution to my problem.

  • Silly question here, but Nginx? Apache? What versions? – df2k2 Apr 15 '18 at 16:35
1

Check if mod_rewrite is enabled on your server. That can be done by reading the phpinfo(); from a script on your server.

Use the below code in .htaccss

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Other way is

  1. Log-in Magento Admin
  2. Go to Stores-> Configuration -> Web
  3. From Search Engine Optimisation tab Use Web Server Rewrites select YES.
  4. Make sure your Secure and Unsecure base urls should end with “/”.

Remove cache and check.

  • I have ensured Server Rewrites is selected YES and ensured Secure and Unsecure urls end with '/'. But still it is not working. Is it that I am in developer mode.?Should I be changing that.According to your Rewrite rule "my_url/index.php/" should ideally go to "my_url write". Cleared cache,still not working.. – Lisa Augustine Sep 23 '16 at 9:48
1

First, QUERY_STRING is everything after the question mark, so it should be ^route=.+$ instead of ^index.php?route=.+$. If you want to test for index.php as well, use a second condition with REQUEST_URI.

So instead of

RewriteCond %{QUERY_STRING} ^index.php?route=.+$ [NC] 

it would be

RewriteCond %{QUERY_STRING} ^route=.+$ [NC] 
RewriteCond %{REQUEST_URI} ^index.php$ [NC] 

Second, the rewrite rule would rewrite /index.php?route=x to /route=x. Is that really what you want? In case you actually need /x, use pattern matching:

RewriteCond %{QUERY_STRING} ^route=(.+)$ [NC] 
RewriteCond %{REQUEST_URI} ^index.php$ [NC] 
RewriteRule ^(.*)$ /%1\? [R=301,L]

%1 matches the first sub-pattern in RewriteCond, just as $1 matches the first sub-pattern in RewriteRule.

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.