Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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_.htaccess_Mod Rewrite - Fatal编程技术网

.htaccess重写更改错误url

.htaccess重写更改错误url,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我在我的.htaccess中做了一些重定向的东西,它工作得很好 RewriteEngine On # add www. if missing RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] # redirect index.html and index.php to the simple homepage RewriteCond %{

我在我的.htaccess中做了一些重定向的东西,它工作得很好

RewriteEngine On
# add www. if missing
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# redirect index.html and index.php to the simple homepage
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\|/index\.html\ HTTP/
RewriteRule ^index\.php$|^index\.html$ http://www.example.com/ [R=301,L]
# add trailing slash if missing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
# redirect simulated directories to php files of the same name in /content/
RewriteCond %{REQUEST_URI} !^(\/css|\/images) [NC]
RewriteRule ^([^/]+)/$ content/$1\.php?rw=1 [QSA,L]
RewriteCond %{QUERY_STRING} !^rw=1
RewriteRule ^content/([^/]+).php$ /$1/ [R=301,L]
但是,虽然启用了添加尾部斜杠的部分,但URL中存在一个奇怪的问题,这些URL不存在,并且指向我的404错误页面: 例如,如果输入www.example.com/notexisting/该url将转换为www.example.com/content/notexisting.php/?rw=1
在htaccess中禁用尾部斜杠部分或模拟目录部分时,不会发生这种情况。但我想两者都保留。有可能吗?

通过在“斜杠”重定向中添加额外条件,您似乎可以很容易地解决问题。您似乎没有任何以.php结尾的url,因此您可以检查您的url是否已经以该结尾。如果不是这样,可以添加斜杠

RewriteEngine On

# add www. if missing
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# redirect index.html and index.php to the simple homepage
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\|/index\.html\ HTTP/
RewriteRule ^index\.php$|^index\.html$ http://www.example.com/ [R=301,L]

# add trailing slash if missing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^.*[^/]$ /$0/ [L,R=301]

# redirect simulated directories to php files of the same name in /content/
RewriteCond %{REQUEST_URI} !^(\/css|\/images) [NC]
RewriteRule ^([^/]+)/$ content/$1\.php?rw=1 [QSA,L]

RewriteCond %{QUERY_STRING} !^rw=1
RewriteRule ^content/([^/]+).php$ /$1/ [R=301,L]

很 完美!就这样!谢谢。