问题
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