.htaccess将每个php文件的.php替换为/


.htaccess replace .php with / for every PHP file

我正在尝试让以下功能在没有框架的情况下在PHP中工作。我不想为我所做的每一个PHP应用程序都设置一个超级复杂的框架

http://domain.com/sign_up.php becomes http://domain.com/sign_up/

http://domain.com/user.php?id=432 becomes http://domain.com/user/?id=432

或者,如果有一种方法可以将其变成http://domain.com/user/432,但我不确定如何在该场景中处理多个$_GET变量,所以这是可选的。

到目前为止效果很好:

RewriteRule ^sign_up/([^/]*)$ /sign_up.php?p=$1 [L]

唯一的问题是,我必须对我使用的每一个php文件都这样做,这可能会变得很多。

对所有php文件来说,通用的方法是什么?

更新:

这一行运行得很好:

RewriteRule ^(.*)'/$ $1.php [NC]

唯一的问题是它不能自动重定向PHP

例如,我想301自动重定向:

http://domain.com/file.php to http://domain.com/file/

http://domain.com/file.php?var1=value&var2=value to http://domain.com/file/?var1=value&var2=value

如果有人能想出一种更好的方法来以更SEO友好的方式处理查询字符串值,那就太棒了!但除此之外,到目前为止,这项工作非常有效。

更多更新:

现在,这正在发挥作用:http://domain.com/file/-至-http://domain.com/file.php

这两个都指向具有以下htaccess代码的同一页面:RewriteRule ^(.*)'/$ $1.php [NC]

然而,没有尾随/http://domain.com/file返回未找到页面错误。

此外,我需要知道如何将http://domain.com/file.php自动重定向到http://domain.com/file/

主要工作HTACCESS

这个.htaccess工作得很好:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}  !'.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteRule ^(.*)'/$ $1.php [NC]

它唯一不做的是自动重定向,如果它们直接转到http://domain.com/file.php,它不会重定向到http://domain.com/file/,但它的其他一切都在工作。

试试这个:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f # but php exists
RewriteRule ^([^/]+)/([^/]+)?$ $1.php?p=$2 [L]

然而http://domain.com/file没有尾随/返回一个找不到页面的错误。

这是因为除非末尾有/,否则您的规则不匹配。

RewriteRule ^(.*)'/$ $1.php [NC]
                  ^

您可以将?作为进行选择

RewriteRule ^(.*?)/?$ $1.php [L]

注意,/之前不需要'。它可以使用或不使用它。

我还需要知道如何自动重定向http://domain.com/file.php到http://domain.com/file/

# Rewrite original .php request to new URLs
RewriteCond %{THE_REQUEST} ' /([^.]+)'.php [NC]
RewriteRule ^ /%1/ [R,L]
# Resolve the new URLs to .php files
RewriteRule ^(.*?)/?$ $1.php [L]

如果您先实现这一点,我们可以稍后查看如何处理查询参数。


你的最终htaccess可能看起来像

# Rewrite original .php request to new URLs
RewriteCond %{THE_REQUEST} ' /([^.]+)'.php [NC]
RewriteRule ^ /%1/ [R,L]
# Force a trailing / if not a file
RewriteCond %{REQUEST_URI} !'..{3,4}$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
# Redirect to php if not an existing dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]

您可能想要这样的东西:

RewriteEngine on
RewriteBase /
# redirect with trailing parameter
RewriteRule ^(['w]+).php?p=(['w]+)$ $1/$2/  [QSA,R=301]
# redirect bare php files
RewriteRule ^(['w]+).php$ $1/  [QSA,R=301]
# make sure it's not a request to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# make sure we have a trailing slash
RewriteRule ^(.+[^/])$ $1/  [QSA,R=301]
# internally point to the right file
RewriteRule ^([^/]+)/([^/]*)/?$ $1.php?p=$2 [QSA,L]

[R=301]附件将浏览器重定向到具有301、永久移动状态标头的新URL。这样,浏览器就可以知道将来在哪里可以找到正确的url,而无需询问服务器。

此外,有时.htaccess检查器也很有用:http://htaccess.madewithlove.be/请注意,该工具不适用于%{REQUEST_FILENAME}

如果您希望domain.com/help加载domain.com/help.php,并且仍然加载所有jpg/png/css/img等,请使用以下命令:

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