1

Hello I'm trying to hide html and php extension from my site URLs and here's my .htaccess file

RewriteEngine On RewriteCond %{DOCUMENT_ROOT}/install -d RewriteCond %{REQUEST_URI} !(install) [NC] RewriteRule ^(.*) /install/ [L,redirect=302] RewriteCond %{REQUEST_URI} ^(.+)\!$ RewriteRule ^(.*) stats.php?url=$1 [L] RewriteCond %{REQUEST_URI} ^(.+)\~s$ RewriteRule ^(.*) stats.php?url=$1 [L] RewriteCond %{REQUEST_URI} ^(.+)\~q$ RewriteRule ^(.*) generate_qr.php?url=$1 [L] RewriteCond %{REQUEST_URI} ^(.+)\~p$ RewriteRule ^(.*) preview_url.php?url=$1 [L] RewriteCond %{REQUEST_URI} !^(.+)\.html$ RewriteCond %{REQUEST_URI} !^(.+)\.htm$ RewriteCond %{REQUEST_URI} !^(.+)\.php$ RewriteCond %{REQUEST_URI} !^(.+)\.js$ RewriteCond %{REQUEST_URI} !^(.+)\.css$ RewriteCond %{REQUEST_URI} !^(.+)\.jpg$ RewriteCond %{REQUEST_URI} !^(.+)\.png$ RewriteCond %{REQUEST_URI} !^(.+)\.gif$ RewriteCond %{REQUEST_URI} !^(.+)\.swf$ RewriteCond %{REQUEST_URI} !^(.+)\.xml$ RewriteCond %{REQUEST_URI} !^(.+)\.ico$ RewriteCond %{REQUEST_URI} !^(.+)\.txt$ RewriteCond %{REQUEST_URI} !^index\.html$ RewriteCond %{REQUEST_URI} !^index\.php$ RewriteCond %{REQUEST_URI} !^/$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d #RewriteCond %{REQUEST_URI} !^(.+)/$ RewriteRule ^(.*) url_redirector.php?url=$1 [L] RewriteRule ^(.*).html$ $1.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^.]+)$ $1.html [NC,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] 

Any ideas how can i do that? I've tried a lot of answers that I found but it's not working out for me.

2 Answers 2

3

You can refactor your rules to reduce lot of redundancy and have 2 additional rules for hiding .php and .html extensions:

RewriteEngine On RewriteCond %{DOCUMENT_ROOT}/install -d RewriteRule !^install/ /install/ [L,NC,R=302] RewriteRule ^(.+(?:!|\~s))$ stats.php?url=$1 [L,QSA] RewriteRule ^(.+\~[pq])$ preview_url.php?url=$1 [L,QSA] RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.+?)/?$ $1.html [L] RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.+?)/?$ $1.php [L] RewriteCond %{REQUEST_URI} !\.(php|js|css|jpe?g|png|gif|swf|ico|xml|txt|html?)$ [NC] RewriteCond %{REQUEST_URI} !^/index\.(php|html)$ [NC] RewriteCond %{REQUEST_URI} !^/$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* url_redirector.php?url=$0 [L,QSA] 
Sign up to request clarification or add additional context in comments.

1 Comment

Just to let you know, what you got in other answer is actually not correct. It is not checking presence of .php or .html file before adding the extension. At present, it won't rewrite example.com/about to example.com/about.php
1

Removing Extensions

To remove the .php extension from a PHP file you have to add the following code inside the .htaccess file:

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

If you want to remove the .html extension from a html file

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

To remove .html

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

Updated

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

6 Comments

i put this RewriteRule ^([^\.]+)$ $1.html [NC,L] in my htaccess but not working i put it in the last line
i have tested its working fine for me if i have html.are you planning to hide all extension ?
@MounerMostafa.i have updated and its working fine.first yuo have to write rule for html and then php
see my update question i update htaccess file with ur code i put it in the last line but still not working
thanks man i just used that wrong now it's working . u just save my day many thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.