【问题标题】:.htaccess Remove first php url parameter if second exist.htaccess 如果第二个存在,则删除第一个 php url 参数
【发布时间】:2021-10-12 09:20:55
【问题描述】:

这是我的 .htaccess 代码:

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

*其中 $1 = 个人资料,$3 = john-smith

https://example.com/profile/john-smith 这样的重写效果很好,但我需要像 https://example.com/john-smith 这样的第二个重写规则,只有当第二个参数包含 约翰史密斯存在。

谢谢!

附言。 (我在 .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 redirect mod-rewrite url-rewriting friendly-url


    【解决方案1】:

    使用您展示的示例,请尝试以下操作。用显示的示例/尝试编写。请确保您的 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
    

    【讨论】:

    • 非常感谢您的回复!不幸的是,您获取example.com/john-smith 的解决方案不起作用。
    • @Adrian,所以链接example.com/john-smith 应该由pages.php?PAGE=$1 提供,对吗?请确认一次。
    • 不。该值(例如 john-smith)来自 LINK 参数
    • @Adrian,不,我想确认您在浏览器中点击链接 https://example.com/john-smith 并且它正在被重写(由 page.php 提供在后端?请确认一次吗?
    • 是的,它在浏览器地址字段中按照我的意愿重写了 url,但页面没有加载正确的内容。
    猜你喜欢
    • 2019-12-12
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多