Mod_rewrite - change .htaccess into httpd.conf file

房东的猫 提交于 2021-01-28 09:47:07

问题


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]

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 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.


回答1:


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

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]


来源:https://stackoverflow.com/questions/39400674/mod-rewrite-change-htaccess-into-httpd-conf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!