简体   繁体   English

.htaccess 删除 PHP 扩展与重定向尾部斜杠的冲突

[英].htaccess remove PHP extension conflicts with redirect trailing slash

I have three pieces of code.我有三段代码。

Here's the first one:这是第一个:

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

The second one:第二个:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R,NE]

The third one:第三个:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-_]+)/$ profile/index.php?username=$1 [QSA,L]
  1. The first removes the .php extension from the URL bar第一个从 URL 栏中删除 .php 扩展名
  2. The third shows the contents of a directory.第三个显示目录的内容。 For example, example.com/billgates/ shows the page content of: example.com/profile/index.php?username=billgates例如example.com/billgates/显示的页面内容为: example.com/profile/index.php?username=billgates
  3. The second adds a trailing slash and redirects the non-trailing slash of the #2.第二个添加尾部斜杠并重定向#2 的非尾部斜杠。 For example, example.com/billgates redirects to example.com/billgates/ .例如, example.com/billgates重定向到example.com/billgates/

All codes work but just not very nicely together.所有代码都可以工作,但不能很好地结合在一起。 Here is the problem:这是问题所在:

If I go to example.com/login it removes the .php extension and it works.如果我访问example.com/login它会删除 .php 扩展名并且它可以工作。 BUT the problem is, if I manually add a trailing slash at the end of /login/ , it redirects to: example.com/login/.php/ but it should redirect me to /login because it is not a directory and just a page.但问题是,如果我在/login/的末尾手动添加一个斜杠,它会重定向到: example.com/login/.php/但它应该将我重定向到/login因为它不是一个目录而只是一个页。 But if /login/ was a directory, then it shouldn't redirect.但是如果/login/是一个目录,那么它不应该重定向。


Full .htaccess:完整的.htaccess:

RewriteEngine On

ErrorDocument 404 /404.php

...the three pieces of codes above

Have it this way and test after clearing browser cache:这样做并在清除浏览器缓存后进行测试:

ErrorDocument 404 /404.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule /$ - [L,R=404]

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


RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ profile/index.php?username=$1 [QSA,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM