Rewrite rule for magento store view - .htaccess

i have a question about .htaccess redirection rule. Namely, i have two store views in magento: english and swedish. I want to write rule in .htaccess that when user type in this address in browser url bar www.example.com/se to be redirected to https://example.com/se/
Can someone help me od give me some pointers for this problem?
Thanks

you can write like this
RewriteCond %{HTTP_HOST} =www.example.com
RewriteRule (.*) http://example.com/$1 [R=302,L]

Related

htaccess RewriteRule for new domain

have some problems with my .htaccess file. I have two domains, old one and new one. i want to redirect user to the new site, when they visit the old domain.
Example:
if they visit: "olddomain.com" then they should get redirected to "newdomain.com". this is actually not that hard to code BUT i want some special redirects too. if someone trys to visit "olddomain.com/service.html" then i want redirect the user to "newdomain.com/whatwedo.hml".
And if someone trys to visit "olddomain.com/test/hello/text.html" then i want to redirect him to "newdomain.com/something/hello.html".
i have a list of 20 specific links i want to redirect.
what i've tried so far:
RewriteEngine On
RewriteCond %{HTTP_HOST} (www\.)?olddomain.com/service.html
RewriteRule (.*) http://www.newdomain.com/whatwedo.hml [R=301,L]
RewriteCond %{HTTP_HOST} (www\.)?olddomain.com
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]
with this code every visit on olddomain.com/whatever will redirect to "newdomain.com"
can someone help me?
thank you :)
Greetings
%{HTTP_HOST} will contain only the domain name used in browser address bar (olddomain.com) but not the URI (/service.html), so your first condition will never be matched. Try this:
RewriteCond %{HTTP_HOST} (www\.)?olddomain.com
RewriteRule ^service\.html http://www.newdomain.com/whatwedo.hml [R=301,L]

mod_rewrite: query string gets lost on rewrite

I have limited .htaccess knowledge and requires some help. I need to redirect all page request to www.newdomain.com except for www.olddomain/page.json but the query string get dropped when it redirect. how can i preserve it? thankyou very much!
Current code:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/page.json
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
Edit: Just to make it clear, i only need to stay on page.json on the old domain, lets say user request for www.olddomain.com/page1.json?session=gVgr30ExUlM
i need to redirect to
www.newdomain.com/page1.json?session=gVgr30ExUlM
BUT when it is www.olddomain.com/page.json?=LKJHGF i need it to stay on that old domain and wont redirect is it possible?
Use the [QSA] flag ("query string append")
You need to add [QSA] and optionally [NE] which should give you:
RewriteRule (.*) http://newdomain.com/$1 [QSA,NE,R=301,L]

How to redirect a first time visitor using htaccess

How to redirect a first time user to a special landing page using htaccess based on referrer? I mean if they came from another domain then they are the first time visitor?
I am really noob at url rewriting and explanation would be great .
Note: the landing page is nothing but a php script that detects browser. On that page I will use cookie, but need to redirect the user if the referrer is empty or its from another domain.
I suggest this :
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
The first RewriteCond check if referer contains your domain name, and the second check if you are not just redirected by the RewriteRule.
The RewriteRule brings you to the welcome page as a [L]ast rule.
How about redirect the use if his referer is not your domain ?
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
That means that the user will be redirected to welcome.html if he writes example.com in the address bar or comes from a link in another site. Once on your site it won't be redirected anymore if he load another page in your site.
P.S. AFAIK you can use cookies in PHP that generates a plain html page see here
Edit: Update tested code
Excuse my reheating the old steak once more.. I would still be interested in knowing if anyone knows the solution to this problem - without using cookies or HTML5 features...
I have read here that the HTTP_REFERER might be blank. Is that why this method of redirecting is not good for this application? I have experimented with this on my server but the closest result working result was being always redirected to my landing page index.htm, which is not desired..
Could this rule interfere with other rewrite rules?
Also, there is an error in the former snippet:
And I think the NC flag in the latter snippet does not make sense. Should it not be L?
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
#missing space after .com and before [----------------here----^
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
#Should this flag not be L? ------------------------------^

URL Rewriting for url with countrycode

I am writing a multi language website. Therefore I would like some help with a URL rewrite problem.
Case:
When someone visits www.example.com without adding a country code (nl, en, de) the htaccess redirects the visitor to www.example.com/nl/ i.g.
RewriteRule !(nl|en|de)(.*).* /nl/ [R=301,L]
The website is renewed and has got many url's directing to the website (google, forums). i.g. www.example.com/oldpage-nomore.html. What I would like is the following; the htaccess should detect that the request uri doesn't contain nl,en or de and should redirect to pagenotfound.php. RewriteRule ^(.*)\.html /public/oldurl?section=nl&notfound=$1$2&basehref=true&%1 [PT,L] the problem with this Rewrite rule is: all files ending in .html are being redirected.
What I am looking for is the following:
When someone visits www.example.com and no request uri is entered this should redirect to www.example.com/nl/
When there is a requested uri and this doens't contain a countrycode (nl|en|de) than redirect to pagenotfound.php
I tried the following but it doens't work:
RewriteCond %{REQUEST_URI} !^(nl|en|de)$
RewriteRule ^([a-z]{2})/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]
I hope someone can help.
Thank you in advance.
Try to place this .htaccess file at the server root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^.*$ /nl/ [R]
RewriteCond %{REQUEST_URI} !^/(nl|en|de)
RewriteRule ^.*/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]
(you probably made several mistakes, I tried to fix them)
I'm not sure with the first RewriteCond %{REQUEST_URI} regular expression - maybe remove the question mark or slash, I don't now... can't test now.

Multilanguage site, subdirectories as language (RewriteRule)

For my website i want to rewrite my url to:
www.xxx.com/en/index.php instead of www.xxx.com/index.php?language=en
www.xxx.com/nl/index.php instead of www.xxx.com/index.php?language=nl
www.xxx.com should be www.xxx.com/en/
This actually works i have added these rewrite rules
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.xxx\.com(\/?)$
RewriteRule (.*) http://www.xxx.com/en/$1 [R=302,L]
RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]
The R=302 is added for testing purposes
Are the above given RewriteCond and RewriteRule good enough or are there other, maybe shorter or better ways to rewrite .com/ to .com/en/
If the url is edited to a language that does not exists i want this user to be redirect to english.
Example
www.xxx.com/es/index.php is requested, this language does not exists or no language is given i want the user to be redirect to www.xxx.com/en/index.php
I tried the following:
RewriteRule ^([^n][^l]|[^e][^n])/(.*)$ en/$2%{QUERY_STRING} [L,R=302]
This works in some cases but if www.xxx.com/ne/index.php is entered it is considered as valid and not rewritten. www.xxx.com/index.php is also not rewritten to www.xxx.com/en/index.php
Could someone help me to fix this?
Thanks in advance!
Try this rule:
RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]

Resources