rewrite my url, remove query string and make new pretty url

半城伤御伤魂 提交于 2019-12-12 03:12:39

问题


I want to rewrite my url from http://website.com/preview.php?id=puzzled to http://website.com/cv/puzzled and from http://website.com/resume.php?id=puzzled to http://website.com/puzzled ... but seems i am failed to get this result.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([0-9a-zA-Z-]+) /resume.php?id=$1 [QSA,L]
RewriteRule ^cv/([0-9a-zA-Z-]+) /preview.php?id=$1 [QSA,L]

回答1:


You can use these rules:

Options +FollowSymLinks
RewriteEngine on

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

RewriteRule ^([\w-]+)/?$ resume.php?id=$1 [QSA,L]

RewriteRule ^cv/([\w-]+)/?$ preview.php?id=$1 [NC,QSA,L]



回答2:


Try it like this,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^cv/([\w-]+)$ preview.php?id=$1
RewriteRule ^([\w-]+)$ resume.php?id=$1
RewriteRule ^([\w-]+).php$ $1 [L]

From Apache mod_rewrite docs

The variables SCRIPT_FILENAME and REQUEST_FILENAME contain the same value - the value of the filename field of the internal request_rec structure of the Apache HTTP Server. The first name is the commonly known CGI variable name while the second is the appropriate counterpart of REQUEST_URI (which contains the value of the uri field of request_rec).

If a substitution occurred and the rewriting continues, the value of both variables will be updated accordingly.

If used in per-server context (i.e., before the request is mapped to the filesystem) SCRIPT_FILENAME and REQUEST_FILENAME cannot contain the full local filesystem path since the path is unknown at this stage of processing. Both variables will initially contain the value of REQUEST_URI in that case. In order to obtain the full local filesystem path of the request in per-server context, use an URL-based look-ahead %{LA-U:REQUEST_FILENAME} to determine the final value of REQUEST_FILENAME.



来源:https://stackoverflow.com/questions/39387631/rewrite-my-url-remove-query-string-and-make-new-pretty-url

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