mod_rewrite for removing .php extension is producing 404 error

与世无争的帅哥 提交于 2019-12-12 04:55:46

问题


I've looked up and tried 3 or 4 answers on SO for this and they are not working for me.

I have dev.mydomain.com/pw/ and I want dev.mydomain.com/pw/builder to redirect to dev.mydomain.com/pw/builder.php etc.

I have tried several solutions including:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

from: here and:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

from: here

as well as:

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

that I have from other projects.

I have also tried manipulating the RewriteBase to various things, like / and /pw/ to no effect.

Anything obvious I'm doing wrong/not doing?


回答1:


Place this rule in /pw/.htaccess:

RewriteEngine On
RewriteBase /pw/

RewriteCond %{THE_REQUEST} /(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

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


来源:https://stackoverflow.com/questions/25337282/mod-rewrite-for-removing-php-extension-is-producing-404-error

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