要做到这一点:
原始URL -> localhost/viewprofile.php
ReWrite URL -> localhost/viewprofile/
我用了这个密码。
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>为了做这件事我用
原始URL -> localhost/viewprofile.php?user_id = 1
ReWrite URL -> localhost/1/
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ viewprofile.php?user_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewprofile.php?user_id=$1但不幸的是我想这么做
原始URL -> localhost/viewprofile.php?user_id = 1
ReWrite URL -> localhost/viewprofile/1/
和这个
原始URL -> localhost/viewprofile.php?username=sean
ReWrite URL -> localhost/viewprofile/sean/
我有这两个Original URLs,我想像上面给出的那样将它们分别重写为各自的ReWrite URL。
有什么解决办法吗?
发布于 2015-12-26 15:47:58
您可以在数字(仅)上匹配第一个数字和另一个数字(例如)的字母数字。
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.+)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)/$ $1.php [L]
RewriteRule ^viewprofile/([0-9]+)/?$ viewprofile.php?user_id=$1 [L]
RewriteRule ^viewprofile/([A-Za-z0-9]+)/?$ viewprofile.php?username=$1 [L]
</IfModule>https://stackoverflow.com/questions/34472765
复制相似问题