user3052443
user3052443

Reputation: 842

Htaccess redirect for single page not working

My .htaccess file has redirect and rewrite code. It all works fine except for one page. I need to redirect https://example.com/shopping/ceylon-cinnamon-c-62.html and http://example.com/shopping/index.php?cPath=62 to https://example.com/ceylon-cinnamon-c-2_19.html I have tried the four lines under the NONE OF THESE WORK below (one at a time) but the redirect never works. The result url is https://example.com/c-62.html. Can anyone point out the problem or how to test it?

 Options +FollowSymLinks -Indexes
 RewriteEngine On 
 RewriteBase /

 # NONE OF THESE WORK
 Redirect 301 /shopping/ceylon-cinnamon-c-62.html https://example.com/index.php?cPath=2_19
 Redirect 301 /shopping/index.php?cPath=62 https://example.com/index.php?cPath=2_19
 RewriteRule ^(.*?)shopping/ceylon-cinnamon-c-62.html$ https://example.com/index.php?cPath=2_19 [R=301,L,NC] 
 RewriteRule ^(.*?)shopping/index.php?cPath=62$ https://example.com/index.php?cPath=2_19 [R=301,L,NC] 

 # THESE ALL WORK 
 RewriteRule ^shopping/(.*)$ /$1 [R=301,NC,L]

 #redirect index.php to root
 RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
 RewriteRule ^index\.php$ https://example.com/ [R=301,L]

 RewriteCond %{SERVER_PORT} 80
 RewriteRule ^(.*)$ https://example.com/$1 [R,L]

 RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}

Upvotes: 0

Views: 230

Answers (1)

arkascha
arkascha

Reputation: 42875

Your question is a bit vague... The example rules you give ("NONE OF THESE WORK") have little to do with what you ask in your question. And the claimed result of the rewriting attempts certainly is not what your attempts implement. So either your description is incorrect (written from memory maybe?) or you have some other factor in place which you do not tell us about (some application logic maybe that implements its own redirection?)...

Anyway, here are the simple rules to implement the exact redirection you ask about, independent of any other stuff:

RewriteEngine on

RewriteCond %{QUERY_STRING} (?:^|&)cPath=62(?:&|$)
RewriteRule ^/?shopping/index\.php$ /ceylon-cinnamon-c-2_19.html [R=301]

RewriteRule ^/?shopping/ceylon-cinnamon-c-62\.html$ /ceylon-cinnamon-c-2_19.html [R=301]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...

That said: when testing make tripple sure that you are not looking at cached results. Always use a fresh anonymouse browser window when testing, make deep reloads , not just reloads and watch your browser networking console for the actual response you receive...

This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Upvotes: 0

Related Questions