为什么我的 .htaccess 文件在本地主机上工作但在服务器上不工作?

Why is my .htaccess file working on localhost but not on the server?

我上传到网络服务器的 .htaccess 文件在本地主机(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= [NC,L]
RewriteRule ^user/(.*)$ /account.php?param2= [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 ^(.+)$ /.php [NC,L]

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

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

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

我想我找到了解决办法。

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

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

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

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