Rewrite an URL but keep the original one - .htaccess

I need to keep the original URL from my subdomain to a specific page :
I tried the following but not working :
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^my-subdomain.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/page1/ [R=301,NC,L]
I get : http://domain.com/page1/
I want : http://my-subdomain.domain.com

I think this is what you want:
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteCond %{REQUEST_URI} ^/page1 [NC]
RewriteRule ^(.*)$ http://my-subdomain.domain.com/ [R=302,L]
Input
http://domain.com/page1/
Output
http://my-subdomain.domain.com/

Related

Convert links to another domain

I want convert links to another domain
From this : www.old.net/upload/namefolder/namefile.mp3
To : www.new.net/upload/namefolder/namefile.mp3
using .htaccess
How can I do this?
Try this hope it will work fine.
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(?:mp3)$
RewriteCond %{HTTP_HOST} old\.net [nc]
RewriteRule ^(.*)$ http://www.new.net/$1 [R=301,L,QSA]
You can use something like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.old\.net$ [NC]
RewriteRule ^(.*)$ http://www.new.net [R=301,L]

Multiple rewrite rules to different pages

I am trying to create multiple rewrite rules, so that a few pages will be redirected to certain pages, and the rest will be redirected to the start page. However, all my pages keep getting redirected to the start page.
This is the code I am using:
RewriteCond %{HTTP_HOST} ^site\.com/category\.php?s=1$ [NC]
RewriteRule (.*) http://site.co.uk/category/? [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule (.*) http://site.co.uk/? [R=301,L]
Edit:
This is the full .htaccess:
Order deny,allow
DirectoryIndex default.php index.php
SetEnv DEFAULT_PHP_VERSION 5
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php?\s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
This is the link I am trying to access: www.site.com/flavours.php?s=1
HTTP_HOST cannot match REQUES_URI.
You can use:
# specific redirects
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php\?s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?flaverco\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
Make sure to clear your browser cache before testing this.

change to https and redirect to subfolder rule in .htaccess

I am trying to do two things here:
redirect to a sub-folder
redirect http://www.something.com/some/page.html
or
https://www.something.com/some/page.html
to
https://www.something.com/subfolder/some/page.html
redirect http to https
redirect
http://www.something.com/subfolder/some/page.html
to
https://www.something.com/subfolder/some/page.html
And I want to do both of them in the same .htaccess file
I have been able to redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
And then I am trying to do both of them; i.e. http to https(only if http request comes) and redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
But it's not working.
What am I doing wrong here?
EDIT
When using #starkeen's solution; i.e.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ https://www.example.com/subfolder/$1 [R=301,NC,L]
I am expecting the following as result:
https://www.example.com/subfolder/brands/omega.html
when I give any of the following:
http://example.com/brands/omega.html OK
https://example.com/brands/omega.html OK
http://www.example.com/brands/omega.html OK
https://www.example.com/brands/omega.html OK
http://example.com/subfolder/brands/omega.html WRONG
http://www.example.com/subfolder/brands/omega.html WRONG
But the last two are redirecting to
https://www.example.com/subfolder/
Here is a rule to do both the tasks in a single rule:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(?:subfolder)?(/.*)?$ https://www.example.com/subfolder$1 [NE,L,R=302,NC]
Make sure to clear your browser cache before testing this rule.
Try :
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#--Http ==>https--#
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
#--exclude the destination to avoid redirect loop--#
RewriteCond %{REQUEST_URI} !^/subfolder
#--redirect /foo to /subfolder/foo--#
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC,L]
Clear your browser'cache before testing this redirect.

mod_rewrite not works with http:// but not http://www

I have the following code:
#### Redirect to www location
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sspconf.com [NC]
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R=301,NC]
When I type in http://sspconf.com/ it re-directs to https://www.sspconf.com/ which is fine.
However if I type http://www.sspconf.com/ (with a www) it doesn't re-direct. Can someone help?
Change your rule to:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sspconf.com [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R=301,NC]
So that it redirects if it is missing the "www" OR if it isn't HTTPS.
Add this line:
RewriteCond %{HTTPS} off
and change this line:
RewriteCond %{HTTP_HOST} ^sspconf.com [NC]
to this:
RewriteCond %{HTTP_HOST} ^(www\.)?sspconf.com [NC]
The ^ only matches the start of the string, so www.sspconf.com is a non-match.
So, your overall rule will look like this:
#### Redirect to www location
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?sspconf.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R=301,NC]
Edited to address comment
The following has solved my problem, thanks for the help.
# Force SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.sspconf.com/$1 [R,L]

htacces rewriteRule extensive url's

I moved a multi store Magento website to an other domain.
For redirecting all the page, I used a htaccess file.
The old domains are:
www.olddomain.nl -> www.newdomain.nl
www.olddomain.nl/categoryone -> www.newdomaincategoryone.nl
www.olddomain.nl/categorytwo -> www.newdomaincategorytwo.nl
www.olddomain.nl/categorythree -> www.newdomaincategorythree.nl
This is working, but when the path is more extensively, it does not work anymore.
For example:
http://www.oldomain.nl/categoryone/subcategory/product to
http://www.newdomaincategoryone.nl/subcategory/product
The htaccess code is:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*olddomain\.nl [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.nl [NC]
RewriteRule ^categoryone/?$(.*) http://www.newdomaincategoryone.nl/$1 [R=301,NC]
RewriteRule ^categorytwo/?$(.*) http://www.newdomaincategorytwo.nl/$1 [R=301,NC]
RewriteRule ^/?$(.*) http://www.newdomain.nl [R=301,NC]
</IfModule>
You rules and regex are incorrect.
You can use this code:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.nl$ [NC]
RewriteRule ^categoryone(/.*)?$ http://www.newdomaincategoryone.nl$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.nl$ [NC]
RewriteRule ^categorytwo(/.*)?$ http://www.newdomaincategorytwo.nl$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.nl$ [NC]
RewriteRule ^/?$ http://www.newdomain.nl [R=301,NC,L]

Resources