【问题标题】:.htaccess mod_rewrite.htaccess mod_rewrite
【发布时间】:2010-10-02 12:25:13
【问题描述】:

您好,我正在努力做到这一点,以便当您访问我的网站时,您不必将 .php 放在最后,这就是我正在使用的,但它不起作用(godaddy 托管)

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

我今天刚刚添加了“Options +FollowSymlinks”,但它仍然不起作用。 非常感谢

【问题讨论】:

  • 您确定 mod_rewrite 已启用且正常工作吗?你尝试过其他规则吗?
  • 我是新手,如何启用 mod_rewrite
  • @Matthew Carter:尝试一个非常简单的规则,例如RewriteRule ^ http://example.com/?
  • 我有自定义错误页面,如果这可能会影响任何事情
  • @Matthew Carter:有没有同名的目录,所以可能不满足第一个条件?

标签: php html .htaccess mod-rewrite


【解决方案1】:

我还不知道“FollowSymLinks”是做什么的,但其余的都是这样做的:

RewriteEngine On            <-- activates mod rewrite
RewriteCond %{REQUEST_FILENAME} !-d  <-- condition that says: request filename is not directory
RewriteCond %{REQUEST_FILENAME}\.php -f  <-- condition that says: request filename with the appendix .php is a file
RewriteRule ^(.*)$ $1.php  <-- take anything you get and put a .php behind it

用人类的话来说: 如果请求的文件名不是目录,并且您将 .php 附加到该文件名并且它是现有文件,则执行将 .php 附加到请求的文件的重写规则

这适用于我的 XAMPP:

<IfModule mod_rewrite.c>
    RewriteEngine On            
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

【讨论】:

    【解决方案2】:

    我会这么说

    RewriteCond %{REQUEST_FILENAME}\.php -f
    

    是不必要的。那是为了什么?

    【讨论】:

    • 这将避免无限递归,因为只有可以积极映射到现有 .php 文件的请求才会被重写。
    • 绝不是不必要的!它可以防止像 test.html 这样的东西被重写为 test.html.php!
    猜你喜欢
    • 2011-01-12
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2012-11-05
    • 2011-11-17
    • 2011-05-04
    相关资源
    最近更新 更多