简体   繁体   中英

rewriteCond request with htaccess

I would like to accomplish the followings (rewrite not redirect):

  1. searchPage.php?crs_category=variable like searchPage.php?crs_category=business to category/business or category/business/ case in-sensitive

You can also directly access category/name which is the rewrite of searchPage

  1. index.php/name to page/name case in-sesitive. note this is a rewrite not a redirect and i would like access the index.php/name just with a clean url.

You can also directly access page/name which is the rewrite of index.php/name

Note: - it needs to take into account spaces such as if a user click on searchPage.php?crs_category=html%20and%css it would rewrite the url to category/html and css

Tried:

RewriteEngine On
RewriteBase /

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


RewriteCond %{THE_REQUEST} /searchPage.php\?crs_category=([^\s]+) [NC]
RewriteRule ^category/%1? [NE,NC,R,L]
RewriteRule ^category/([^/]+)/?$ /searchPage.php?crs_category=$1 [QSA,L,NC]


RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ /page/%1 [NE,R,L]
RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]

Problem: For some reason the request doesn't work for these lines:

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

In addition, I am not sure who take into account white space. where if its searchPage.php?crs_category=html%20andcss it leads to category/html and css disregarding the spacing code

Update:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]

# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]

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

RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ page/%1 [NE,R,L]

RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category=$1 [QSA,L,NC]

RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]

You should be matching / after index.php not ? :

You can use these rules:

RewriteEngine On
RewriteBase /

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

RewriteCond %{THE_REQUEST} /index\.php/(\S+) [NC]
RewriteRule ^ page/%1 [NE,R,L]

# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^(\S*)\s+(.*)$ $1-$2 [E=NOSPACE:1,DPI]

# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^(\S+)$ $1 [NE,R=302,L]

RewriteRule ^category/([^/]+)/?$ searchPage.php?crs_category=$1 [QSA,L,NC]

RewriteRule ^page/([^/]+)/?$ index.php/$1 [NC,L]

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