Redirect urls with query string to seo-friendly directory like urls

三世轮回 提交于 2019-12-11 11:37:59

问题


I know this question is asked by many users, also I gone through many questions to find the solutions as I am unable to understand the code used in htaccess file. So please kindly help me to solve this issue. I just want to convert a link like:

www.abc.com/project.php?proj-cat=some+project+name

to url like:

www.abc.com/project/some+project+name

OR to like:

www.abc.com/project/some-project-name/

I googled and found a website which creates a htaccess rule for you, using this site I got this:

RewriteEngine on
RewriteRule project/proj-cat/(.*)/ project.php?proj-cat=$1 [L]

but this doesn't redirect to the links above. Please help me to find the solution for this and help me understand how it works. Thank You!


回答1:


You can use this code in root .htaccess:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+project\.php\?proj-cat=([^\s&]+) [NC]
RewriteRule ^ project/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^project/([^/.]+)/?$ project.php?proj-cat=$1 [L,QSA,NC]

Reference: 1. Apache mod_rewrite Introduction

2. http://askapache.com



来源:https://stackoverflow.com/questions/25061324/redirect-urls-with-query-string-to-seo-friendly-directory-like-urls

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