简体   繁体   中英

Redirect all php extension to none extension with .htaccess

I have set a rule to remove php extension now I want to redirect url with php extension to url without php extension

EX: mysite.com/test.php redirect to mysite.com/test mysite.com/test.php? redirect to mysite.com/test? mysite.com/test.php? redirect to mysite.com/test? mysite.com/test.php# redirect to mysite.com/test#

I have lots of php page so I want a rule for all pages and my php pages generate dynamic URL also.

Thanks

I assume you are using Apache,

You can use following;

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

And be sure that above rule, will respond 404 if file not exists, or it is a directory.

When you type domain.com/tes this will be act domain.com/test.php at backend.

If you want to redirect user when he uses domain.com/test.php to domain.com/test you can use;

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

If Apache, then In your Virtual host directory of Apache settings you need to add following code

Options Indexes FollowSymLinks MultiViews

And then restart Apache, in this case you don't have to redirect each page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM