.htaccess删除第一个php url参数,如果第二个存在



这是我的。htaccess代码:

RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))? pages.php?PAGE=$1&LINK=$3 [L]
*where $1 = profile and $3 = john-smith

这可以很好地重写为https://example.com/profile/john-smith,但我需要第二个重写规则,如https://example.com/john-smith,只有当第二个参数包含john-smith

存在。谢谢!

p。(额外的规则,我在上面我的。htaccess文件)

# protect files beginning with .
RewriteRule /.(.*) - [NC,F]
# redirect HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# No root access without index.* and other security
RewriteEngine On
Options All -Indexes
RewriteBase /
DirectoryIndex index.php index.html index.htm
ErrorDocument 404 https://example.com/pages.php?PAGE=404
# Prevent upload malicious PHP files
<FilesMatch “.(php|php.)$”> 
Order Allow,Deny 
Deny from all 
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

使用显示的示例,请尝试以下操作。用显示的示例/尝试编写。请确保您的htaccess规则文件与pages.php文件一起存在。

请确保在测试url之前清除浏览器缓存。

RewriteEngine On
Options All -Indexes -Multiviews
RewriteBase /
# protect files beginning with .
RewriteRule ^. - [NC,F]
# redirect HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)/?$ https://www.example.com/$1 [R,L]
##Rules for uri https://example.com/john-smith goes from here.
RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ pages.php?PAGE=$1 [QSA,L]
##Rules for uri https://example.com/profile/john-smith goes from here.
RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ pages.php?PAGE=$1&LINK=$2 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
# No root access without index.* and other security
# Prevent upload malicious PHP files
<FilesMatch ".(php|php.)$"> 
Order Allow,Deny 
Deny from all 
</FilesMatch>
DirectoryIndex index.php index.html index.htm
ErrorDocument 404 https://example.com/pages.php?PAGE=404

相关内容

最新更新