要做到这一点:
原始URL->localhost/viewprofile.php
重写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
重写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
重写URL->localhost/viewprofile/1/
和这个
原始URL->localhost/viewprofile.php?username=sean
重写URL->localhost/viewprofile/sean/
我有这两个Original URLs
,并且我想如上所述将它们分别重写为各自的ReWrite URL
。
有什么解决方案吗?
您可以为第一个匹配数字(仅),为另一个匹配字母数字(例如)
<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>