Magento htaccess Rewrite and Redirect - regex

On a Magento based ecommerce site I am trying to remove index.php from URLs as well as 301 redirect non-www to www.
Default URL: www.example.com/index.php/super-cool-product.html
Desired Product URL: www.example.com/super-cool-product.html
Also 301 redirecting non-www to www:
example.com/super-cool-product.html
to:
www.example.com/super-cool-product.html
As well as:
www.example.com/index.html
to:
www.example.com
This is what I currently have:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.rejuvahealth.com/$1 [R=301,NC,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.rejuvahealth.com/$1 [R=301,NC,L]

To 301 redirect index.html:
Options +FollowSymLinks
RewriteEngine on
redirect 301 /index.html http://www.example.co.uk/
If you configure your Base URLs correctly, Magento should automatically redirect to the www. version.
For index.php rewrites, go to Configuration > Web > Use Web Server Rewrites and change to 'Yes'.
Now open your htaccess and change this line:
#RewriteBase /magento/
to this:
RewriteBase /
Assuming that your Magento folder is on the root. Be careful when using web rewrites though and ensure you back up before making any changes.

Related

Redirect non-www to www giving me hundreds of 404's

Thanks to anyone who can take a moment to look at this.
Recently I created a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
In this folder I have a htaccess file to which I have added.
## No directory listings
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
When I try to access say http://example.com/subdomain/anytrailingstring then it's NOT redirecting me to http://www.example.com/subdomain/anytrailingstring as I expected, it is redirecting to http://www.example.com/anytrailingstring leaving out the /subdomain/ and this is of course a page that doesnt exist and therefore a 404.
This is a problem.
I do not have any directive in the root .htacces file except for this :
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can someone perhaps see why the subdomain htaccess isnt redirecting to correctly? Did I miss something?
I am not good with htaccess at all, if anybody can help me I would really appreciate it.
Thanks!
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You need to use the REQUEST_URI server variable instead of the backreference ($1). The URL-path matched by the RewriteRule pattern (first argument) is relative to the current directory, so excludes the parent subdirectory (ie. /subdomain in your example).
Do it like this instead:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You will need to clear your browser cache since the erroneous (301 - permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
However, a couple of questions:
Why are you not using HTTPS? (You are redirecting to HTTPS in the parent .htaccess file - but this is now being overridden by the mod_rewrite directives in the subdirectory.)
Why not include this in the parent .htaccess file?
UPDATE: So, taking the above points into consideration... if you want to move this rule to the parent .htaccess file in the root then have it like this:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The order of the directives is to ensure there is only ever at most 1 redirect (assuming you are not implementing HSTS).
You were unnecessarily duplicating the RewriteEngine directive (so I removed the second instance).
The RewriteBase directive was not being used.
The capturing subgroup in your HTTP to HTTPS rule was not required. ie. ^ is better than ^(.*)$ in this instance.
Aside:.
...a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
This is a subdirectory, not a "subdomain".
This is a "subdomain":
http://subdomain.example.com/

.htaccess Redirect 301 with ? (without variables)

There are some urls with ?.
/shop/index.php?route=affiliate/register
/shop/?route=affiliate/register
It is needed for these urls to be redirected to:
/another-page/
(asume all urls begin with the home directory. example: www.homepage.com/shop?route=affiliate/register )
I have tried both simple apache Redirect 301 and rewrite.
You may use this rule as your top rule in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /shop/(?:index\.php)?\?route=[^\s&]+ [NC]
RewriteRule ^ /another-page/? [R=301,L,NE]

.htaccess redirect www to non-www not working

I pretty much know nothing about about .htaccess editing or regex expressions. What I have done to try to make my site re-direct from www versions to non-www versions is I followed the answers posted here:
redirect www to non-www
However, what I get is that if I enter www.example.com, I get:
example.com/example.com/ and a 404 error on my site
This is my .htaccess file:
#Adjust default time zone
SetEnv TZ America/Toronto
#Begin redirect www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
# Google Analytics Integration - Added by cPanel.
<IfModule mod_substitute.c>
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|(<script src='/google_analytics_auto.js'></script>)?</head>|<script src='/google_analytics_auto.js'></script></head>|i"
</IfModule>
# END Google Analytics Integration
Can someone enlighten me on what I am doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
That is, check if the HTTP_HOST contains www and if so, redirect to non-www with the request_uri appended.

not able to redirect HTTP to HTTPS using rewrite in htaccess

I have tried url redirection of HTTP to HTTPS on my website by using several different solutions in .htaccess and none have worked.
Well, actually it does work if I type in directly:
(only for example) http://mywebsite.com
into the browser address bar....but if I try to redirect the url any other way, it won't work. If I try the link to my page listed on Google http://mywebsite.com/ it will not redirect to https://www.mywebsite.com. I need it to redirect from HTTP to HTTPS if someone clicks the link to my page from a search engine.
So far I have tried:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
then I tried...
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
and then I tried...
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]
None worked. What else should I try? (this website is being hosted on a web hosting service site that has uses an apache web server). Also, I have an SSL certificate for the page, if that makes any difference.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Tip: Clear your browser cache or use a different browser to test.

.htaccess issue with ExpressionEngine and YOURLS

I have a primary site running ExpressionEngine and I am trying to get YOURLS running in a subfolder called "work", ie: http://foo.com/work/ for short urls and EE is running at the root, ie: http://foo.com/. Please note there is no 'www'.
The problem I am having is that when I use a short URL and a user adds "www" to the URI, such as http://www.foo.com/work/123 I get a redirect chain that looks like this:
http://www.foo.com/work/123 302 redirects to
http://www.foo.com/work 301 redirects to
http://www.foo.com/work/ which returns 200
Everything works fine if you omit the 'www' from the URI. YOURLS is set up to use the non-www but the worry is that some users might habitually type the 'www' as these URIs are being placed on printed ads.
The root .htaccess file looks like this:
SetOutputFilter DEFLATE
DirectoryIndex index.php index.html
# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.foo.com [NC]
RewriteRule ^(.*)$ http://foo.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
</IfModule>
# END ExpressionEngine Rewrite
The /work/ (YOURLS) .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /work/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /work/yourls-loader.php [L]
</IfModule>
# END YOURLS
How can I get the 'www' URIs to work like the non-www URIs do without completely hosing EE? :)
I faced a similar issue. Some end users or people publishing the short urls tend to append them with www while the short url was bought with the intention of using it without www.
I also installed the YOURLS redirect index plugin to forward homepage visitors to our regular homepage.
So I had a few different types of urls:
short domain homepage, should lead to the regular https homepage
short domain YOURLS admin panel, should lead to the https admin panel
short domain shortened urls, should lead to the long url
I wanted all urls to work with or without www and make sure http was forwarded to https.
My solution was to start .htaccess with these lines:
# Force HTTPS (because of passwords via /admin) and use clean domain (non www)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.examp.le [NC]
RewriteRule ^(.*)$ https://examp.le/$1 [L,R=301]