【问题标题】:URL rewrite when .php extension is already hidden.php 扩展名已隐藏时的 URL 重写
【发布时间】:2019-02-18 11:00:34
【问题描述】:

我的 URL 已被重写以隐藏我的 .php 扩展名。我现在在尝试进一步重写 URL 时遇到问题。

我当前的网址(注意查询字符串前的正斜杠):

https://example.com/card2/?search=Jimmmy 

这是我目前尝试过的:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2.php?search=$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2.php\?search=([^\&\ ]+)
RewriteRule ^/?card2\.php$ /card2/%1? [L,R=301,NE]

如果我尝试此 URL,这将有效(我必须在查询字符串之前手动删除正斜杠并输入 .php):

https://example.com/card2.php?search=Jimmy

所以目前我可以让它工作的唯一方法是在 URL 中输入 .php 并在此之后删除正斜杠。

我尝试了以下方法,我的逻辑是删除重写中的 .php 扩展名并添加正斜杠,但它不起作用:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2\/\?search=([^\&\ ]+)
RewriteRule ^/?card2$ /card2/%1? [L,R=301,NE]

编辑,感谢anubhava 的回答,这是我现在的 htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
ErrorDocument 404 /not-found.php

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

<filesMatch "\.( jpg|jpeg|gif|png|ico|js)$">
Header set Cache-Control "max-age=2419200, public, must-revalidate"
</filesMatch>

<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 70.24.57.210

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]

RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
RewriteRule ^ /card2/%1? [L,R=301,NE]

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    让你的重定向规则像这样带有可选匹配:

    RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
    RewriteRule ^ /card2/%1? [L,R=301,NE]
    

    还要将此规则保留在您的 .htaccess 顶部并从新浏览器进行测试。

    【讨论】:

    • 完美运行!虽然我不得不将它添加到我的 .htaccess 文件的底部。如果我将它添加到顶部,我会不断收到 500 错误?我会用我的 .htaccess 内容更新我的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2011-09-08
    • 2012-02-27
    • 2011-11-14
    相关资源
    最近更新 更多