【问题标题】:Why is my .htaccess file working on localhost but not on the server?为什么我的 .htaccess 文件可以在 localhost 上运行,但不能在服务器上运行?
【发布时间】:2020-04-21 14:31:16
【问题描述】:

我上传到网络服务器的 .htaccess 文件在 localhost(wamp 服务器)上运行良好,但没有像我希望的那样工作。

这是我的代码:

RewriteEngine On

RewriteRule ^IMPRESSUM  about.php [NC,L]
RewriteRule ^HOME  welcome.php [NC,L]
RewriteRule ^LOGIN  login.php [NC,L]
RewriteRule ^LOGOUT  logout.php [NC,L]
RewriteRule ^SIGNUP  register.php [NC,L]

RewriteRule ^([a-zA-Z]*)$ /index.php?param1=$1 [NC,L]
RewriteRule ^user/(.*)$ /account.php?param2=$1 [NC,L]

代码的目的应该是不用输入文件扩展名(“/home”而不是“/home.php”)就可以访问php文件。但是,如果用户输入的不是文件名,例如“/something”,则它应该与“/index?param1=something”相同。

在它不起作用后,我尝试了许多我在互联网上找到的解决方案。例如我试过:

RewriteEngine On
RewriteBase /
RewriteRule ^IMPRESSUM  about.php [NC,L]
RewriteRule ^HOME  welcome.php [NC,L]
RewriteRule ^LOGIN  login.php [NC,L]
RewriteRule ^LOGOUT  logout.php [NC,L]
RewriteRule ^SIGNUP  register.php [NC,L]

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

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)$ /index.php?param1=$1 [NC,L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^user/(.*)$ /account.php?param2=$1 [NC,L]

我希望有人知道我的问题的解决方案。 亲切的问候。

【问题讨论】:

    标签: .htaccess mod-rewrite server url-rewriting localhost


    【解决方案1】:

    我想我找到了解决办法。

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([a-zA-Z]*)$ /index.php?param1=$1 [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^user/(.*)$ /account.php?param2=$1 [NC,L]
    

    代码首先查看是否存在名称与斜杠后面的值相等的php文件。如果存在,它将打开文件,如果这样的文件不存在,则将 /something 转发到 /index.php?param1=something。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 2014-07-14
      • 1970-01-01
      • 2021-12-14
      • 2018-08-27
      • 2020-05-12
      • 2013-07-14
      • 1970-01-01
      相关资源
      最近更新 更多