Remove .html Before Redirecting in Htaccess - .htaccess

I have the rule below which works perfectly, however My oldsite urls have .html and me newsite doesn't. Is it possible to strip the .html before redirecting? e.g.
www.oldsite.com/mypage.html
gets redirected to
www.newsite.com/subdir/mypage/
e.g.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.oldsite.com$ [NC]
RewriteRule ^(.*)$ http://www.newsite.co.uk/subdir/$1 [R=301,L]

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.oldsite.com$ [NC]
RewriteRule ^(.*)\.html$ http://www.newsite.co.uk/subdir/$1 [R=301,L]
so www.oldsite.com/mypage.html will be redirected to www.newsite.com/subdir/mypage

Related

add https for subdomains using .htaccess

I have two subdomains like schools.mydomain.co.uk and admin.mydomain.co.uk,I need to add https:// for all request url for both two domains,I have change my .htaccess like as follows.These two subdomains pointing to the same root folder.
but gettiong an error "This webpage has a redirect loop"
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^admin\.mydomain\.co\.uk [NC]
RewriteRule (.*) https://admin.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^schools\.mydomain\.co\.uk [NC]
RewriteRule (.*) https://schools.mydomain.co.uk/$1 [R=301,L]
You are not checking whether they are already being accessed using HTTPS:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} ^off
RewriteCond %{HTTP_HOST} ^(admin|schools)\.mydomain\.co\.uk [NC]
RewriteRule (.*) https://%1.mydomain.co.uk/$1 [R=301,L]

Redirection works BUT

I have set up a redirect from old domain to new domain and it works however there are links which is not redirecting properly....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.us$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.us$
RewriteRule (.*)$ http://www.new.com/$1 [R=301,L]
</IfModule>
I am trying to redirect : www.old.us/scripts/affiliate.pl?id=505 to www.new.com
but redirects like this : www.new.com/scripts/affiliate.pl?id=505 results a 404 page.
Change your code to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old\.us$ [NC]
RewriteRule ^ http://www.new.com/? [R=301,L]

htaccess redirect subdomain

I have the following .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
The problem is that all subdomains get redirected to www.mysite.co/subdomain
How can I aviod this?
Your rewrite rule logic in plain speak is doing the following:
For ANY HOST other thant www.mysite.co (including foo.mysite.com, blog.mysite.com, etc..)
Permanent Redirect (301) to http://www.mysite.co/
This is the last rule to check in the .htaccess file, so bail out
To not redirect your own subdomains, the easiest and clearest way is to handle it explicitly with more rewrite conditions (see example below). For more kung fu htaccess fun try this: .htaccess Wildcard Subdomains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^blog.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^foo.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
You need to run your index.php file
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]

htaccess for subfolders

I'm new htaccess. So i need to rewrite .php to .html in url.
my files located in
http://www.domain.us/sub1/sub2/sub2/index.php
I need to rewrite as
http://www.domain.us/sub1/sub2/sub2/index.html.
I tried something
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} site
RewriteRule ^(.*)\.php$ http://www.domain.us/sub1/sub2/sub3/$1.html [R,L]
But It shows 404 error.
Thanks.
Put this code in your DOCUMENT_ROOT's .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/*sub1/sub2/sub2/.*\.php [NC]
RewriteRule ^(.*)\.php$ /$1.html [NC,NE,R=301,L]

Redirecting from www.host.com to host.com in .htaccess

Can someone please provide me with the proper directives in .htaccess file to have www.host.com requests to be redirected to host.com and vice versa?
This will redirect from http://www.yoursite.com to http://yoursite.com:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^yoursite\.com
RewriteRule (.*) http://yoursite.com/$1 [R=301, L]
And this is the reverse:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]
The above is what I use on my server, and it works perfectly.

Resources