.htaccess to convert query string never works

Deadly 提交于 2019-12-11 14:23:24

问题


The goal: To have a query string converted into a nice looking URL, which I have never gotten to work.

The details: Change this URL: http://www.mywebsite.com/static-directory/index.php?state=florida&city=tallahassee

to this one: http://www.mywebsite.com/static-directory/florida/tallahassee

What I have tried: Well, just about everything. My latest search came up with this tutorial...

I have my .htaccess file in my root directory (httpdocs).

Any help or point me at the right answered question on Stack Overflow.

Thanks.


回答1:


QUESTION:
"Change this URL: .../static-directory/index.php?state=florida&city=tallahassee
to this one: .../static-directory/florida/tallahassee"

According to the question, all is needed is to convert the 1st URL (The Request) with the query to the 2nd one without a query.

Here is a way to achieve that in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /static-directory/index.php\?state=([^&]+)&city=([^\s&]+) [NC]
RewriteRule  ^static-directory/index.php  /static-directory/%1/%2? [R=301,L,NC]

However, just in in case the OP forgot to mention the whole idea is to remove the query in order to have a "pretty" URL but still get the data from the original URL, add next 2 lines to the rule-set:

RewriteCond %{REQUEST_URI}  !index\.php [NC]
RewriteRule  ^static-directory/([^/]+)/([^/]+) /static-directory/index.php?state=$1&city=$2 [L]


来源:https://stackoverflow.com/questions/16780531/htaccess-to-convert-query-string-never-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!