1

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.

2 Answers 2

1

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] 
Sign up to request clarification or add additional context in comments.

9 Comments

thanks, but it is not working for me. URL in the browser localhost/demo/order/?oid=123&stat=open and it should internally work like localhost/demo/order.php?oid=123&stat=open
That is exactly it is doing. If you enter URL as: http://localhost/demo/order.php?oid=123&stat=open it gets redirected to http://localhost/demo/order/?oid=123&stat=open and that is internally rewritten to /demo/order.php?oid=123&stat=open (if /demo/order.php file exists)
I tried this in localhost localhost/custom/hello/?hello=world, It says object not found (404). hello.php file is there in the custom folder.
If this .htaccess is placed in DocumentRoot then it should load /custom.hello.php file. Try this rule in EDIT and also provide me error line from error.log when you get 404
I have placed the htaccess file in the "custom" folder not on the root. I have updated the htaccess file with the edit you have given. Still not working. URL is rewriting as I expected like localhost/custom/hello/?hello=world, but giving 404 object not found error.
|
0

Try this:

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

1 Comment

Can you please change the below code to what I am expecting RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA] This is working but I need to append trailing slash at the end of the file name with query string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.