Mod_rewrite-将.htaccess更改为httpd.conf文件 [英] Mod_rewrite - change .htaccess into httpd.conf file

查看:77
本文介绍了Mod_rewrite-将.htaccess更改为httpd.conf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循以下.htaccess规则,该规则可将URL中的.php扩展名从domain.com/index.php删除到domain.com/index/,并且工作正常

I have following .htaccess rules which removing .php extension in urls from domain.com/index.php to domain.com/index/ and working fine

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

现在,我想使用httpd.conf文件而不是.htaccess,但是当我尝试将其重写为:

Now I want to use httpd.conf file instead of .htaccess, but when I try rewrite it to:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -f
RewriteRule ^/(.*?)/?$ $1.php [NC,L]

URL已正确重写为domain.com/index/,但服务器返回错误404.

url is rewritten correctly to domain.com/index/, but server returns error 404.

如果有什么帮助和建议,我会很高兴.

I'll be glad for any help and suggestions, what could be wrong.

推荐答案

%{REQUEST_FILENAME} 已经是完整的文件系统路径,因此请在其前面加上%{DOCUMENT_ROOT}将创建无效的路径.

%{REQUEST_FILENAME} is already full filesystem path if file or directory is found so prefixing that with %{DOCUMENT_ROOT} will make an invalid path.

您可以在 httpd.conf 中使用这些规则:

You can use these rules in httpd.conf:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^ %1/ [R=301,NE,L]

RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/(.+?)/?$ /$1.php [L]

这篇关于Mod_rewrite-将.htaccess更改为httpd.conf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆