Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

apache - Rewrite Rule in htaccess convert query string into slashes php

OK I've tried multiple solutions but still can't achieve it. so what I need to do is

mydomain.com/search?q=product&l=london

to

mydomain.com/search/product/london

Basically I'm using rewrite rules to hide .php extension by following this code

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^([^.]+)$ $1.php [NC,L]

So now, search.php converts into search and when user searches. it redirects to trailing slash instead of query string

Here what I've used so far

RewriteCond %{THE_REQUEST} /search/?q=([^&]+)&l=([^s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]

#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d

 #skip the rule if the request is for file
 RewriteCond %{REQUEST_FILENAME} !-f
 #rewrite any other request  to "/packagemenu.php?"
 RewriteRule ^([^/]+)/([^/]+)/?$ /search?q=$1&l=$2 [NC,L]

AND

RewriteBase /search/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:.php)??q=([^s&]+)&l=([^s&]+) [NC]
RewriteRule ^ search/%1/%2/? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /search(?:.php)??q=([^s&]+)s [NC]
RewriteRule ^ search/%1/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal forward from pretty URL to actual one
RewriteRule ^search/([^/]+)/?$ search?q=$1 [NC,L,QSA]

RewriteRule ^search/([^/]+)/([^/]+)/?$ search?q=$1&l=$2 [NC,L,QSA]

But both of them doesn't work. I don't have so much knowledge about so please help me out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have your site root .htaccess as this:

Options -MultiViews
RewriteEngine On
RewriteBase /jobportal/

# To externally redirect /search.php?q=product&l=london to /search/query/london/
RewriteCond %{THE_REQUEST} /search(?:.php)??q=([^s&]+)&l=([^s&]+)s [NC]
RewriteRule ^ search/%1/%2/? [R=301,L,NE]

# To externally redirect /search.php?q=product to /search/query/
RewriteCond %{THE_REQUEST} /search(?:.php)??q=([^s&]+)s [NC]
RewriteRule ^ search/%1/? [R=301,L,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} s/+(.+?).phps [NC]
RewriteRule ^ %1 [R=301,NE,L]

# ignore all rules below for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal forward from /search/product/ to search.php?q=product
RewriteRule ^search/([^/]+)/?$ search.php?q=$1 [NC,L,QSA]

# internal forward from /search/product/london/ to search.php?q=product&l=london
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&l=$2 [NC,L,QSA]

# internal forward from /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...