.php扩展名已隐藏时重写URL

时间:2019-02-18 11:00:34

标签: .htaccess mod-rewrite

我有一个已经被重写以隐藏我的.php扩展名的URL。我现在在尝试进一步重写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]

1 个答案:

答案 0 :(得分:1)

让您的重定向规则具有可选的匹配项:

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

此规则也应放在您的.htaccess顶部,并通过新的浏览器进行测试。