【问题标题】:How do I allow .php, trailing slash, and no trailing slash?如何允许 .php、斜杠和没有斜杠?
【发布时间】:2011-07-30 11:16:45
【问题描述】:

如何允许 .php、斜杠和没有斜杠?

RewriteEngine on

RewriteCond %{REQUEST_URI} (.*)(\.php|\/)$
RewriteRule (.*)$ %1 [R=301,L]

DirectoryIndex index.php
#directory ban
Options All -Indexes

ErrorDocument 400 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

FileETag MTime Size
ExpiresActive on
ExpiresDefault "access plus 86400 seconds"

# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 2 DAYS
<FilesMatch "\.(xml|txt|css|js)$">
Header set Cache-Control "max-age=172800, proxy-revalidate"
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>

<IfModule mod_expires.c>
# any Expires Directives go here
</IfModule>

<IfModule mod_headers.c>
# any Header directives go here
</IfModule>

<ifmodule mod_deflate.c="">
<filesmatch ".(js|css)$"="">
SetOutputFilter DEFLATE
</filesmatch>
</ifmodule>
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteRule ^(.*)$ mydomain.com/$1 [R=301,L]

【问题讨论】:

    标签: .htaccess url mod-rewrite clean-urls


    【解决方案1】:

    试试这个修改后的 .htaccess 文件:

    RewriteEngine on
    DirectoryIndex index.php
    Options All -Indexes
    
    ErrorDocument 400 /error.php
    ErrorDocument 404 /error.php
    ErrorDocument 500 /error.php    
    ## doesn't work for me
    ##FileETag MTime Size
    ##ExpiresActive on
    ##ExpiresDefault "access plus 86400 seconds"
    # 1 WEEK
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
    </FilesMatch>
    # 2 DAYS
    <FilesMatch "\.(xml|txt|css|js)$">
    Header set Cache-Control "max-age=172800, proxy-revalidate"
    </FilesMatch>
    # 1 MIN
    <FilesMatch "\.(html|htm|php)$">
    Header set Cache-Control "max-age=60, private, proxy-revalidate"
    </FilesMatch>
    <ifmodule mod_deflate.c="">
    <filesmatch ".(js|css)$"="">
    SetOutputFilter DEFLATE
    </filesmatch>
    </ifmodule>
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond  %{QUERY_STRING} !^myvar=0
    RewriteCond %{REQUEST_URI} !\..*$
    RewriteRule ^([^/]*)/?$ /$1.php?myvar=0 [QSA,L]
    

    【讨论】:

    • 嗯,现在除了 index.php 之外的所有网页都无法使用。起初,当我尝试访问任何非索引页面时,我被重定向到我的 domain.com/error 并出现错误“此网页具有重定向循环”。然后,我从 .htaccess 中删除了我的“errordocument /error.php”。现在我只是得到一个断开的链接。我认为重定向现在可能会产生一些影响。有什么办法可以让所有的,.php,斜杠,没有斜杠,所有的工作?
    • 现在我得到的最好的结果是“# BEGIN NoExtensions RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule (.*)/?$ $1.php # END NoExtensions”。尾随斜杠不适用于此...
    • 根据您的问题,您正在尝试制作/foo.php OR /foo/ to /foo 这是正确的吗?
    • 是的,我想这样做,所以无论该人输入 /foo、/foo/ 还是 /foo.php 都没有关系。所有人都会将人引导到同一页面。
    • 上述规则实际上已经在这样做了(我也测试过)。但是,当最终 URI 变为 /foo 时,它将得到 404,除非它以某种方式知道您实际上是在为 URI /foo 提供 /foo.php。我今天请你看看我的回答:stackoverflow.com/questions/5544919/… 这可能正在做你想做的事。
    【解决方案2】:

    尝试以下规则以使其根据您的要求工作,

    RewriteCond %{REQUEST_URI} (.*)(\.php|\/)$
    RewriteRule (.*)$ %1 [R=301,L]
    

    如果它不起作用,请告诉我..

    【讨论】:

    • 嗯,我在使用 anubhava 的代码时遇到了同样的问题。我实际上想要 .php、/ 和没有斜线现在可以工作(没有重定向)。你能告诉我这怎么可能吗?有关更多信息,请查看我对 anubhava hah 的回复
    • 上述规则的工作原理与您在问题中提到的完全一样,即/foo.php --&gt; /foo/foo --&gt; /foo 我也尝试进入我的本地域......并且它的工作......
    • 嗯,它不起作用:/。我张贴了我的整个 htaccess。也许有什么东西打断了它?
    • RewriteCond %{HTTP_HOST} !mydomain\.com$ [NC] RewriteRule (.*)$ mydomain.com/$1 [R=301,L] 替换最后一行
    • 现在,当我转到 domain.com/subsection.php、domain.com/subsection 和 domain.com/subsection/ 时,我被重定向到 /error 并出现错误“此网页有Chrome 上的重定向循环”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2021-08-06
    相关资源
    最近更新 更多