简体   繁体   中英

How to edit .htaccess to remove .php

All of my pages end in .php, I need these pages to still be able to run PHP even though the extension has changed in the url bar. I want a page like

website.com/page.php

To

website.com/page

I have looked at half a dozen work arounds to this problem but none of them seem to work. Here are some examples.

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

RewriteEngine On
RewriteRule \/([^\/]+)\/$ $1.php

# once per htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-_]+)/?$ /$1.php
RewriteCond %{REQUEST_URI} !^/shopName1.php$
RewriteRule ^([a-z0-9-_]+).php$ /$1/ [R]
RewriteRule ^shops/shopName1/?$ /shopName1.php
RewriteRule ^shopName1.php$ /shops/shopName1/ [R]

Hide the .php Extension

RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

Explanation How this work

Ans. This rule will match url.com/path/ and check, if url.com/path.php exists. If file exists then process ahead to rewrite rule apply.

Currently I used this rule Work perfect, Hope this help you!


Updated

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

in the above code the -d define for Directory and -f for Regular File.

This rule will same as above but its support Alphanumeric URL . The above code is Tested and Work Fine with my server files.

Example: If your filename is "www.url.com/path1.php" then your may access this file directly with "www.url.com/path1" .

尝试这个

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\\.]+)$ $1.php [NC,L]

Just make a directory called page and rename page.php as index.php and put in in /page. If you need dynamic directories (like /product5 runs details.php?productid=5), then an .htaccess solution would be useful.

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