3

i've to give some information about my website Environment

  1. i have static webpage in the root.

  2. Wordpress installed in sub-dictionary www.domain.com/blog/

  3. I have two .htaccess , one in the root and one in the wordpress folder.

i want to

  • www to non on all URLs < below code DID it :)
  • Remove index.html from url < below code DID it :)
  • Remove all .html extension / Re-direct 301 to url without .html extension < below code DID it :)
  • Add trailing slash to the static webpages / Re-direct 301 from non-trailing slash << I NEED HELP WITH THAT
  • Force trailing slash to the Wordpress Webpages / Re-direct 301 from non-trailing slash < below code DID it :)

Some examples

domain.tld/index.html >> domain.tld/

domain.tld/file.html >> domain.tld/file/

domain.tld/file.html/ >> domain.tld/file/

domain.tld/wordpress/post-name >> domain.tld/wordpress/post-name/

My code in ROOT htaccess is

 <IfModule mod_rewrite.c> Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / #removing trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ $1 [R=301,L] #www to non RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC] RewriteRule .? http://%1%{REQUEST_URI} [R=301,L] #html RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^\.]+)$ $1.html [NC,L] #index redirect RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ RewriteRule ^index\.html$ http://domain.com/ [R=301,L] RewriteCond %{THE_REQUEST} \.html RewriteRule ^(.*)\.html$ /$1 [R=301,L] </IfModule> <IfModule mod_rewrite.c> RewriteEngine on RewriteBase /blog/ # Force Trailing Slash for wordpress RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)[^/]{1}$ %{REQUEST_URI}/ [L,R=301] </IfModule> 

The above code do

  1. redirect www to non-www
  2. Remove trailing slash at the end (if exists)
  3. Remove index.html
  4. Remove all .html
  5. Redirect 301 to filename without trailing slash at the end
  6. Force Trailing Slash for wordpress and redirect 301 from non trailing slash at the end

EDIT

#removing trailing slash Rule usage 

removing trailing slash Rule

12
  • See also RFC 2606. Commented Aug 3, 2016 at 15:23
  • Just to understand one of your examples. Do you want to access domain.tld/file/ on your browser and the server sends you the file domain.tld/file.html ? Commented Aug 7, 2016 at 3:03
  • i want to force trailing slash and redirect 301 domain.tld/file to domain.tld/file/ Commented Aug 7, 2016 at 3:12
  • Ok, but what about this example you wrote: "domain.tld/file.html >> domain.tld/file/" ? Do you want to access domain.tld/file/ on your browser and the server sends you the file domain.tld/file.html ? Commented Aug 7, 2016 at 3:16
  • @André Bonna The original URL was domain.tld/file.html and the above code stripped the html and redirect 301 to domain.tld/file , Now i want to redirect it to domain.tld/file/ Commented Aug 7, 2016 at 3:17

2 Answers 2

5

Have it this way for site root .htaccess:

<IfModule mod_rewrite.c> Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / #www to non RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC] RewriteRule ^(.+?)/?$ http://%1/$1/ [R=301,L] RewriteCond %{THE_REQUEST} \s/+(.+?)\.html/?[\s?] [NC] RewriteRule ^ /%1/ [R=301,NE,L] #index redirect RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L] # add a trailing slash to non files RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE] # add html internally RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^.]+)/$ $1.html [L] </IfModule> 

Make sure to clear your browser cache before testing.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks @anubhava but it's not working for ghadaalsaman.com/poem-list.html << it gives 500 error, ghadaalsaman.com/en/poem-list << and for that file , Please open redirect-checker.org/index.php and check how many redirects for that page ghadaalsaman.com/books.html
how to reduce the redirects and make it 301 not 302 Thanks in addvanced
No it is redirecting with 301 when I requested http://ghadaalsaman.com/books.html/. You have old browser cache issue.
As you can see redirect rule is same for all *.html so if one is showing 301 and another .html is showing 302 then it is due to some other reason not the rule.
it works after i've added that line RewriteCond %{REQUEST_URI} !sitemap [NC] Thank you
|
3
RewriteEngine On RewriteBase / 

WWW to Non

RewriteCond %{HTTP_HOST} ^www.domain\.tld$ [NC] RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301] 

Remove index.html from url

RewriteRule ^index.html$ / [L,R=301] 

Remove all .html extension / Re-direct 301 to url without .html extension

RewriteRule ^([a-zA-z0-9]+).html$ /$1 [L,R=301] 

Add trailing slash to the static webpages / Re-direct 301 from non-trailing slash

RewriteRule ^([a-zA-z0-9]+).html$ /$1/ [L,R=301] 

Force trailing slash to the Wordpress Webpages / Re-direct 301 from non-trailing slash

RewriteRule /blog/([^/]+) /blog/$1/ [L,R=301] 

3 Comments

Thanks for your reply but your solution seems to be not working.
@GhadaAlSamman Where do you place the .htaccess for the code in this answer?
In root .htaccess File

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.