我有一个去除文件扩展名的 URL 重写。它正在使用 .html 页面,但它给了我一个带有 .php 文件的“404 Page Not Found”错误。

这是我完整的 .htaccess 文件

# The following will allow you to use URLs such as the following:
#
#   example.com/anything
#   example.com/anything/
#
# Which will actually serve files such as the following:
#
#   example.com/anything.html
#   example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.

Options +FollowSymLinks
RewriteEngine On

rewritecond %{HTTP_HOST} ^inaflashgraphics.com$
rewriterule ^ "http\:\/\/www\.inaflashgraphics\.com\/" [R=301,L] #4e2f2fa615667

# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]

# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

我究竟做错了什么?

最佳答案

我想通了,这段代码对我有用。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

rewritecond %{HTTP_HOST} ^inaflashgraphics.com$
rewriterule ^ "http\:\/\/www\.inaflashgraphics\.com\/" [R=301,L]

RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

关于.htaccess - URL 重写适用于 .html 但不适用于 .php,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13609064/

10-13 01:29