简体   繁体   中英

With htaccess remove .php extension from any file name, add trailing slash with query string

I want to remove .php extension from given any file name with htaccess and add trailing slash with query string. It should work in localhost also.

Case 1: 

http://localhost/demo/order/?oid=123&stat=open (in the browser)
to 
http://localhost/demo/order.php?oid=123&stat=open (internal)

Case 2: 

http://mydomain.com/order/?oid=123&stat=open (in the browser)
to
http://mydomain.com/order.php?oid=123&stat=open (internal)

It should work for any file name like order.php, contact.php, member.php, ...

I tried this, it is working but I want to add trailing slash at the end of the file name with query string

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

I tried many, but not exactly reached to my requirement. Like remove .php extension, add trailing slash, add query string. But not all these in one .htaccess file. I want all these in one .htaccess file as specified in the above requirement.

Plz help me. Thanks in advance.

You can have this code in root .htaccess:

RewriteEngine On
RewriteBase /custom/

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

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

尝试这个:

RewriteRule ^(.*)\.php$  /$1/ [R=301,L,QSA]

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