简体   繁体   中英

How do I URL Rewrite for below situation?

I have a web page at following url.

https://example.com/contact.php

Now I want to configure the URL redirect like. If user open page with complete URL, it should remove .php from URL automatically.

https://example.com/contact.php ==> https://example.com/contact/

Now the https://example.com/contact/ URL should load content from /contact.php. I am trying below .htaccess but it failed with a redirect loop.

RewriteEngine on

RewriteRule ^contact$ contact.php  [NC,L]
RewriteCond %{REQUEST_URI} !^contact.php$
RewriteRule ^contact.php$ http://example.com/contact/  [R=301]

Use the below code for remove the extention

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(one)/?$ /$1.php [L,NC]

Try the following:

# It redirects the contact.php to /contact/, so .php extension is removed
RewriteRule ^contact\.php$ /contact/ [L,R=301]

# It fetches contact.php when /contact/ is requested by the user
RewriteRule ^/contact/$ /contact.php [L,NC]

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