.htaccess 301 Redirect non slash domains and slash domain to .html -


i trying add code .htaccess redirect slash , non slash urls .html url's apart homepage.

for example www.mydomain.com/cat/ , www.mydomain.com/cat should redirect www.mydomain.com/cat.html

i have managed add following .htaccess redirects www.mydomain.com/cat right place www.mydomain.com/cat.html need on how make slash version redirect .html page

rewritecond %{request_uri} !\.[^./]+$ rewritecond %{request_uri} !(.*)/$ rewriterule ^(.*)$ http://mydomain.com/$1.html [r=301,l] 

my whole .htaccess looks this, if has suggestions on how should in light of above appreciated.

options +followsymlinks rewriteengine on rewritecond %{http_host} ^xxx.xxx.xxx.xx [nc,or] rewritecond %{http_host} ^mydomain.com [nc] rewriterule ^(.*)$ http://www.mydomain.com/$1 [l,r=301]  rewritecond %{the_request} ^.*/index.php  rewriterule ^(.*)index.php$ /$1 [r=301,l]    rewritecond %{request_uri} !\.[^./]+$ rewritecond %{request_uri} !(.*)/$ rewriterule ^(.*)$ http://mydomain.com/$1.html [r=301,l]   directoryindex index.html index.php  <ifmodule mod_rewrite.c> rewriteengine on # pleas note rewritebase setting obsolete use in case experience  problems seo addon. # hostings require rewritebase uncommented # example: # store url http://www.yourcompany.com/store/cart # "rewritebase" should be: # rewritebase /store/cart # rewritebase / rewritecond %{request_filename} !\.(png|gif|ico|swf|jpe?g|js|css)$ rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule . index.php?sef_rewrite=1 [l,qsa]  </ifmodule> 

solved: added:

rewritecond %{request_uri} ^(.+)/$ rewriterule ^(.+)/$  /$1 [r=301,l] 

the separate rules seems working you, think can simplify 1 rule, optional slash. rule redirects slash no-slash, redirects again .html. 1 rule, you'd have 1 redirect.

this has standard rewritecond check if it's not file or folder, doesn't keep redirecting .html if it's one. then, \? in reweriterule optional slash.

rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.*)/?$ http://mydomain.com/$1.html [r=301,l] 

if in domain, can omit result:

rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.*)/?$ /$1.html [r=301,l] 

also, note catch , work subfolders, whether or not mean to. e.g., www.mydomain.com/animals/cat/ redirect www.mydomain.com/animals/cat.html


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -