htacces rewriteRule extensive url's - .htaccess

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]

Related

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.

Rewrite an URL but keep the original one

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/

Need help writing htaccess rule

Currently my localhost URL is like localhost/apnaujjain/page.php?page_id=1&action=contacts
Now I want to write a rule so that when I go to above url it just rewrite the url like localhost/apnaujjain/contacts.html
See it should convert .php into .html without affect page content.
Until now I've tried
Options +FollowSymLinks -MultiViews
RewriteEngine on
#RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.php?page_id=1$&action=2$
RewriteRule ^(.*)\.php $2.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
But it's not working.
You can use this code in /apnaujjain/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/
# redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to
# localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^.]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]

.htaccess : subdomain not mapping correctly

I am trying to make this work:
forum.domain.com -> domain.com/forum
Problem is that this works, but then when I try to access topic of a forum, it says 404.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^forum\.domain\.com\$1$
RewriteCond %{HTTP_HOST} ^www\.forum\.domain\.com\$1$
RewriteCond %{REQUEST_URI} !^http://forum.domain.com/
RewriteRule ^(.*)$ /forum/index.php [R=301,P]
</IfModule>
There are some syntax error and HTTP_HOST only matches domain name without URI.
Replace your code with (assumin mod_proxy is enabled):
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?forum\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/forum/ [NC]
RewriteRule ^ /forum/index.php/$1 [P,L]

Resources