问题
I am trying to use RewriteRule in htaccess to permanently redirect this:
http://www.example.com/store/scripts/prodView.asp?idproduct=240
to
http://www.example.com/index.php?route=product/product&path=59&product_id=50
I have tried:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^idproduct=240$ [NC]
RewriteRule ^store/scripts/prodView.asp$ index.php\?route=product/product\&path\=59\&product_id\=50? [L,NC,R=301]
.htaccess is in the root
Can anyone suggest where I may be going wrong? Thank you
回答1:
Try with:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^idproduct=240$ [NC]
RewriteRule ^store/scripts/prodView\.asp$ index.php?route=product/product\&path=59\&product_id=50 [L,NC,R=301]
Without \?
in RewriteRule url
回答2:
I have solved this problem with the help of Croises although I don't know the full reason for the problem.
In my original question, for brevity, relevance and clarity, I left out some some RewriteRule code that was lying above the code I was writing. This code was provided by the shopping cart software.
The totality of the RewriteRule code was thus:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
#my code start
RewriteCond %{QUERY_STRING} ^idproduct=240$ [NC]
RewriteRule ^store/scripts/prodView\.asp$ index.php?route=product/product\&path=59\&product_id=50 [L,NC,R=301]
#my code end
Croises question below got me thinking “why is my code being ignored?”. I therefore put my code above the shopping cart’ RewriteRule code and now it works, although I don’t understand why the other code was interfering with my code.
The code that now works is:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#my code start
RewriteCond %{QUERY_STRING} ^idproduct=240$ [NC]
RewriteRule ^store/scripts/prodView\.asp$ index.php?route=product/product\&path=59\&product_id=50 [L,NC,R=301]
#my code end
#original code
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
#original code
I guess I have therefore answered my question but with help from Croises. My apologies for leaving out some important information in the original question
来源:https://stackoverflow.com/questions/34569973/htaccess-apache-rewriterule-asp-to-php