简体   繁体   中英

Htaccess 301 redirect with conditions

I have the following code:

RewriteEngine On


# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^category/(.*)$ /searchPage.php?crs_category=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} !^/searchPage.php
RewriteRule ^ index.php [QSA,L]

This works great when I type category/business , but I also want the url to be written if someone type the old url such as searchPage.php?crs_category=business to be redirected as category/business with a 301 code letting the SEO knows that this is the new address.

Note: The reason why there is the

RewriteCond %{REQUEST_URI} !^/searchPage.php

condition is because I am using the Slim framework and where I want all request to be redirected to index with the exception of the searchPage for routing.

Thanks in advance.

Add this right under the commented out RewriteBase /

RewriteCond %{THE_REQUEST} /searchPage\.php\?crs_category=([^&\ ]+)
RewriteRule ^ /category/%1? [L,R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM