magento https/http redirection - mod-rewrite

Is there a way to redirect domains based on its parameters in magento.
Eg:
if user trying to load https://stage.domain.com/ the system should redirect it to
http://stage.domain.com/,
if user trying to load https://stage.domain.com/all_access_profile the system should redirect it to http://stage.domain.com/all_access_profile ,
But the rest can goto its actual link what user actually requested.
I have got this one from #stackoverflow
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This one is always redirecting. How this needs to be changed to get url parameters based redirection.
Thanks

You want to redirect from the secure (https) to the insecure (http) but the rewrite rule http%1 is directing to the secure. That is the wrong way round so it should be:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} ^on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

How do I strip out ?_escaped_fragment_= using .htaccess

Google discovered that I'm allowing end users to navigate my content using ajax loading, and is loading my pages as a user client rather than requesting them as new page loads. So instead of trying to index www.mysite.com/page, it's requesting www.mysite.com/?_escaped_fragment_=/page
Which is not at all what I want it to do. My snapshots are served at the same URL as the ajax-loaded content. The site is not using queries, it's not supporting them and I don't want to build that support. This means that all the pages look broken to google which of course is unfortunate!
Currently all page requests are redirected server side using .htaccess sending requests to the index.php file which in turn compiles the html doc on the server before serving to the client. The site serves perfectly valid and unique html documents for all pages. But google insists on doing it the ajax way and adding the query which always returns a broken page.
I'm not a .htaccess expert, but it seems to me that the easiest way to solve this would be to rewrite the request, remove the ?_escaped_fragment_=/ bit and permanently redirect any such requests to what currently works which is to load the pages using their correct url's.
Anyone know how I would go about doing that? Below is the current redirect part of my .htaccess file which needs to be amended with the _escaped_fragment_ stripping code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#if trailing / remove it with a permanent redirect
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
#if missing www. add it with a permanent redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
#requests for index.php never rewritten
RewriteRule ^index\.php$ - [L]
#if file or directory are missing, route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is how I rewrote it so that all ?_escaped_fragment_=/XXXXX requests got redirected to /XXXXX without the query
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%1? [L,R=301]
This makes www.domain.com/?_escaped_fragment_=/somepage redirect (permanently) to www.domain.com/somepage
...which is just what I wanted.

"If request" in .htaccess?

I use rewrite condition to redirect website always to www. my code is:
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
I can't found on the internet how can i do request if in url '.com' i need it because the website must be also localy accessible.
For example: i found this, but i can't understand how can i implement it with my script.
Your current RewriteCond is correct for applying www. to website.com if it is not already present. To avoid the RewriteRule happening when working on localhost, you need an additional RewriteCond to check the host.
This is because the condition !^www\.example\.com$ matches any domain except www.example.com, which includes localhost.
# Only apply other conditions if not working on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
You can make it a little more dynamic in case you need to work with multiple domains (example.com, example.org) and transform each to www.example.com, www.example.org:
# If not on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
# and the domain does not begin www.
RewriteCond %{HTTP_HOST} !^www\.
# Redirect to apply the www. to HTTP_HOST
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Use caution when testing - your browser may cache old 301 redirects. You may need to change them to R=302 during testing and clear your browser cache. When you are satisfied it works, change it back to R=301.
That RewriteCond should already work for your case, the next rule is processed only if the condition matches, so that rule is not processed for localhost.

301 Redirect Dynamic HOST includes DOCUMENT_ROOT

Based on what I read on Apache I used the following example they provided to do a 301 Redirect on all my web sites.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
This is not quite working as they said it would. If I try www.domain.com it works. If I try domain.com I get www.domain.com//home/www/public_html/www.domain.com
Looks like it wants to include the DOCUMENT_ROOT in the redirect. Am I better off to create an individual .htaccess for each web site?
What is faster to run - Apache or HTACCESS?
Try this instead. Make sure you include the RewriteBase /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you still get the old result, your previous 301 redirect is probably cached, retest in Private (Incognito) Browsing Mode.
Using Apaches httpd.conf is faster since accessing the .htaccess file adds a small overhead—Apache checks every directory—and parent directory—for the file and it will be loaded for every request.
Using the httpd.conf is better when you have access to it. Use .htaccess if you don't have access to the main configuration file.

mod_rewrite secure redirect

Struggling with mod_rewrite trying to redirect a non-secure page to a secure one. This works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://mysite.localhost/%1/ [R=301,L]
But this doesn't:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP:Host} (mysite.localhost|mylivesite.com)
RewriteCond %{REQUEST_URI} (help/returns)
RewriteRule .? https://%1/%2/ [R=301,L]
The URL it tries to give me is https://help/returns//
I can't seem to get the HTTP:host into the final RewriteRule line.
I need the host in there so I can use the same file for local dev and live deployment.
Most grateful for any input.
You can use this rule:
RewriteCond %{HTTPS} =off
RewriteRule ^help/returns https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
This rule will redirect all requests to http://example.com/help/returns to a secure (HTTPS) location: https://example.com/help/returns -- it will preserve full URL path + query string. You have too many conditions, rule becomes complex which is not a good thing when your server is REALLY busy (regular expressions are expensive).
I have replaced %{SERVER_PORT} 80 by more proper %{HTTPS} =off (this especially useful if your site is run on non-default port, which is 80).
I have also removed HTTP_HOST matching part -- you don't really need it unless you have more than one domain name/subdomain bound to the same site. In case if you need this condition just add this line after 1st line: RewriteCond %{HTTP_HOST} ^(mysite.localhost|mylivesite.com)

301 redirect

How do i redirect a url to domain .
eg. http://www.mydomain.com/index.php=HairThing --> http://www.mydomain.com
How do i redirect a non-www to www WITHOUT a slash at end ?
eg http://mydomain.com ---> http://www.mydomain.com
See also: Hidden features of mod_rewrite
#1
RewriteRule /index.php=HairThing$ http://www.mydomain.com [R=301]
#2
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule .* http://www.mydomain.com [R=301]
However, example case 1, as said by Greg, will always put the / on if it is without a uri.
mydomain.com # impossible
mydomain.com/ # possible
mydomain.com/foo #possible
mydomain.com/foo/ #possible
For your second question, the browser will always put a slash after the site name. This is because the trailing slash is required to indicate the root path of the web site.
you could use a general rule that works on every domain without having to change the name of the domain all the time. This is very helpful when you have multiple domains parked on same root.
RewriteCond %{HTTP_HOST} !^www\.[a-z0-9-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z0-9-]+\.[a-z]{2,6})$ [NC]
RewriteRule (.*) http://www.%1/$1 [L,R=301]

Resources