Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 我如何使我的所有URL无扩展,没有尾随斜杠。并将.php和尾部斜杠重定向为none?_.htaccess_Url_Mod Rewrite_Clean Urls - Fatal编程技术网

.htaccess 我如何使我的所有URL无扩展,没有尾随斜杠。并将.php和尾部斜杠重定向为none?

.htaccess 我如何使我的所有URL无扩展,没有尾随斜杠。并将.php和尾部斜杠重定向为none?,.htaccess,url,mod-rewrite,clean-urls,.htaccess,Url,Mod Rewrite,Clean Urls,我想让我所有的URL都统一干净。这意味着我所有的URL都没有扩展名,也没有尾随斜杠,如果一个人输入了.php或尾随斜杠,它只会将此人重定向到干净的URL 例如: example.com/blog/file.php 及 两者都将重定向到 example.com/blog/file 在.htaccess中尝试以下操作: RewriteEngine on #unless directory, remove trailing slash RewriteCond %{REQUEST_FILENAME

我想让我所有的URL都统一干净。这意味着我所有的URL都没有扩展名,也没有尾随斜杠,如果一个人输入了
.php
或尾随斜杠,它只会将此人重定向到干净的URL

例如:

example.com/blog/file.php

两者都将重定向到

example.com/blog/file

.htaccess
中尝试以下操作:

RewriteEngine on

#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
我的web主机最近(2011年9月)将其Apache服务器更新为Apache的较新版本,我的旧
.htaccess
代码显示无扩展URL不再有效。在大量复制代码(可能适用于较旧版本的Apache OS)并测试失败后,我能够确定以下代码似乎适用于我的服务器和其他几个服务器:

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]

试试看,
-Multiviews
似乎是让它工作的关键因素。

这是最好的解决方案,因为你不必硬编码你的域。如果你的网站托管在启用Apache Mod Rewrite moduled的Linux服务器上,你可以使用.htaccess重定向你的文件。此链接[包含有关如何为您的目的格式化文件的信息]()可能会有所帮助。我不认为这适用于我想要的内容,因为我想重定向所有内容,而不是一页。这取决于:您的文件在服务器上的名称如何?有没有分机?
# Turn off the need of directory slash redirecting
DirectorySlash Off

# Turn on RewriteEngine
RewriteEngine On

# Internally redirect request to php file
RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}\.php" -f
RewriteRule "(.*)" "$1.php" [L]

# External redirects
# The above line is used to determine whether a internal redirect is done or not.
# If it is internally redirected, REDIRECT_STATUS will become 200.
# RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"

# Redirect request with .php extension to extensionless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteRule "(.*)\.php$" "$1" [R=301]

# Redirect request with trailing slash to slashless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteCond "%{REQUSET_FILENAME} -d
RewriteCond "%{REQUSET_FILENAME} !-f
RewriteRule "(.*)\/" "$1" [R=301]
# Turn off the need of directory slash redirecting
DirectorySlash Off

# Turn on RewriteEngine
RewriteEngine On

# Internally redirect request to php file
RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}\.php" -f
RewriteRule "(.*)" "$1.php" [L]

# External redirects
# The above line is used to determine whether a internal redirect is done or not.
# If it is internally redirected, REDIRECT_STATUS will become 200.
# RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"

# Redirect request with .php extension to extensionless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteRule "(.*)\.php$" "$1" [R=301]

# Redirect request with trailing slash to slashless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteCond "%{REQUSET_FILENAME} -d
RewriteCond "%{REQUSET_FILENAME} !-f
RewriteRule "(.*)\/" "$1" [R=301]