2

i am trying to remove .php .html and traling slash from url using htaccess,how ever this code working fine for php but not for html and trailing slash

For PHP

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

For HTML

RewriteCond %{REQUEST_URI} index.html
RewriteRule ^(.*)index.html$ /$1/ [R=301,L]
1
  • RedirectMatch 301 ^(.*)/$ /$1 is working for me for trailing slash Commented Jan 31, 2014 at 13:24

1 Answer 1

6

Have it this way:

# externally redirect /dir/file.php to /dir/file and remove index
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:html?|php)/?[\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]

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

# internally add html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Sign up to request clarification or add additional context in comments.

7 Comments

Hi thanks for reply,here the index.html url not working for me when i enter url like mywdesign.com/index.html/web-dev-ecommerce-services it shows page not found,also when i use url like mywdesign.com/index.php/web-dev-ecommerce-services in browser it goes to mywdesign.com//web-dev-ecommerce-services, so double slashes comes. can you please help?
See the update it will fix // problem. However for /index.html/web-dev-ecommerce-services as per requirements it will redirect to /web-dev-ecommerce-services is this not happening? OR do you want to leave it as /index.html/web-dev-ecommerce-services itself?
hi thanks,index.php and trailing slash is working fine now,but index.html not going its not redirecting to /web-dev-ecommece-service however same thing isworking fine with php but in index.html case its not working
That's why I think it is not good idea to strip index.html from /index.html/web-dev-ecommerce-services btw this is really a strange URL. I have seen similar scheme with index.php but not with index.html
the seo guy says when we write in index.html between url it should strip automatically.this is something happens in many websites but here when i write index.php strips using your code but index.html not
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.