我对 apache 中的 Rewrite 不太了解,但是我的 .htaccess 文件有问题。
原来的.htaccess是
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Unless directory, remove trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
ErrorDocument 404 /404.html
第一次重写是针对虚荣网址,另一个是将非 www 重定向到 www,之后将 .php 重定向到扩展名 less 并将 .php 文件重定向到扩展名 less,
我面临的问题是,如果我请求 www.domain.com/ttt.php 并且 ttt 不存在,那么它会将我重定向到 http://www.domain.com/profile?id=ttt.php 另外,我希望重写适用于 php 文件而不是其他类型,假设我有 ttt.png 文件并且我打开 www.domain.com/ttt 它会打开图像而不是显示 404 错误。
其他类型的文件也会发生类似情况。如果名称常见,它会首先打开 .txt 文件。
感谢对此所做的任何更改,说真的,我不想弄乱我的网站。
最佳答案
这条规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]
需要放在最后。
这条规则:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
需要[L]
标志并需要替换RewriteRule ^([^/.]+)$ $1.php [L]
。然后,您可以复制此文件并将 .php
替换为 .png
、.txt
以及您想要处理的所有其他扩展名(按顺序)偏好:
Options +FollowSymLinks -Multiviews
RewriteEngine on
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Unless directory, remove trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L]
# add extension if the php file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# add extension if the png file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png [L]
# add extension if the txt file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt [L]
# add extension if the html file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
# etc.
# finally, route to profile.php if all else fails
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]
ErrorDocument 404 /404.html
由于您要路由到 profile.php
,因此在 profile.php
脚本中,您需要重定向到 /404.html
如果 id
参数不存在(意味着路由的 URI 需要是 404)。
关于apache - 将 .php 重定向到无扩展名并创建没有 .php 的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14030626/