Why is my .htaccess rewrite rule redirecting to the wrong place - .htaccess

This is the my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^(.*)$ https://www.myglobalsite.com/$1 [R,L]
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
redirectMatch 301 ^/products$ https://www.myglobalsite.com/en/product/drinking-cups
redirectMatch 301 ^/products/vending.php$ https://www.myglobalsite.com/en/product/drinking-cups
redirectMatch 301 ^/products/drinking.php$ https://www.myglobalsite.com/en/product/drinking-cups
The issue is when it redirects, it redirects as follows:
from:
https://www.abc-something-old.com/products/vending.php
to:
https://www.myglobalsite.com/products/vending.php
should redirect to:
https://www.myglobalsite.com/en/product/drinking-cups
is the problem in my rewrite rule?

Directives in configuration files (a ".htaccess" fils is a "distributed configuration file") are processed from top to bottom.
The first redirection rule redirects all requests to the same resource path on the new domain. That means that your RedirectMatch directives further down never come into play.
You have to change the order of directives such that specialized, so more precise rules go to the top to get applied first . Also don't forget to correctly type the names of directives, so RedirectMatch instead of redirectMatch. More general rules, so typically catch-all rules go to the bottom to get applied as a last option if no specialized rule got applied before.
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
RedirectMatch 301 ^/products$ https://www.myglobalsite.com/en/product/drinking-cups
RedirectMatch 301 ^/products/vending.php$ https://www.myglobalsite.com/en/product/drinking-cups
RedirectMatch 301 ^/products/drinking.php$ https://www.myglobalsite.com/en/product/drinking-cups
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]
So far you are using a mixture of directives from the alias module and the rewrite module. You could unify that, though both approaches will work:
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^/?products/?$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^/?products/vending\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^/?products/drinking\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]
And by the way: you only need the RewriteCond if both domains are served by the same http server and evaluate the same configuration file to process a request. Otherwise, if those are separate servers or separate virtual hosts with separate DOCUMENT_ROOT folders (which I would strongly recommend), you can simplify that:
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
RewriteEngine On
RewriteRule ^/?products/?$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteRule ^/?products/vending\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteRule ^/?products/drinking\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]

Related

.htaccess file for redirecting directories of subdomain

I have looked at a few of the other .htaccess questions, but none have my specific usecase.
SITUATION
I have a website, say example.com. It has a sub-domain sub.example.com. The web host also provides a folder for every sub-domain: example.com/sub
I want to link both the sub.example.com and example.com/sub to othersite.com/some/folder/path/
What I have so far (the code also adds www things, don't ask why, unless it can fix things):
in /.htaccess:
RewriteBase /
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
##301 Redirect Entire Directory
RedirectMatch 301 /sub/(.*) https://othersite.com/some/folder/path/$1
in /sub/.htaccess:
RewriteBase /sub/
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.example.com[nc]
RewriteRule ^(.*)$ http://www.sub.example.com/$1 [r=301,nc]
##301 Redirect Entire Directory
RedirectMatch 301 /(.*) https://othersite.com/some/folder/path/$1
THE PROBLEM
When I use sub.example.com/foo/bar the redirect works (it redirects to https://othersite.com/some/folder/path/foo/bar)
When I use example.com/sub it breaks (it redirects to https://othersite.com/some/folder/path/sub/foo/bar, notice the ../sub/..)
What do I need to change to make it work so that the sub folder isn't added to the path?
Don't mix mod_alias rules in mod_rewrite. Keep your /sub/.htaccess as
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteBase /sub/
RewriteCond %{HTTP_HOST} ^sub.example.com[nc]
RewriteRule ^(.*)$ http://www.sub.example.com/$1 [r=301,nc]
##301 Redirect Entire Directory
RewriteRule (.*) https://othersite.com/some/folder/path/$1 [L,R=301]
Test this after clearing browser cache.
This sounds like something that mod_dir does because you're accessing a directory without the trailing slash. By default accessing a directory without a trailing slash gets the request flagged for a redirect to the same directory with the trailing slash. This often messes rewrites or other redirects. Try turning it off and adding an additional redirect to handle the trailing slash:
RewriteBase /
DirectorySlash Off
##Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R]
##301 Redirect deviantart Directory
RewriteRule ^sub/(.*) https://othersite.com/some/folder/path/$1 [L,R=301]

htaccess rewrite from subdomain (root) to www (root) without 301 redirect

I am having error in rewriting url. I want the request
http://go.example.com/all-pathnames-flenames
should be handled by some page
http://www.example.com/myfile.php
It should not be hard or 301 redirect (rather original url should be there).
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^go\.example\.com$ [NC]
RewriteRule ^(.*)$ /myfile.php [L]
above mentioned is the code i have but it is not working
You need to exclude rewriting myfile.php, otherwise you'll create a loop:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^go\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/myfile.php
RewriteRule ^(.*)$ /myfile.php [L]

htaccess add index.htm at end of folder

I need a way to add index.htm at end of url.
For example:
www.example.com/folder1/folder2 AND
www.example.com/folder1/folder2/
must be redirected internally to www.example.com/folder1/folder2/index.htm (this must apply for any number of folders/subfolders)
My htaccess (at root) so far looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /
#
# some 301 REDIRECTS here
#
# rewrite non-www into www
# after all redirection rule AND before any rewrite rule
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# rewrite rules below here
#
Any way to fix this?
Thank you.
EDIT:
- All urls come from rewrite rules - there are no actual folders in site.
- Typing www.example.com/folder1/folder2 in my browser, only get a 404 page not found. I need it to redirect to www.example.com/folder1/folder2/index.htm (there is a rule creating this url)
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 %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.htm$ [NC]
RewriteRule ^(.*?)/?$ /$1/index.htm [L,R=301]
Do you mean you want to REDIRECT /folder1 and /folder1/ into /folder1/index.htm, and /folder1/folder2 and /folder1/folder2/ to /folder1/folder2/index.htm, and /folder1/folder2/folder3 and /folder1/folder2/folder3/ to /folder1/folder2/folder3/index.htm
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/%3/index.htm [R]

Rewrite rule to work consistently for subdomain and subdirectory

I have a nice rule for rewriting the url to /meetings, but the rule doesn't rewrite the url properly when visited from a subdirectory.
EDIT: My .htaccess file is in the subdirectory /blog
When I use this rule:...
RewriteRule ^meetings$ /?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): 404 error
When I use this rule:...
RewriteRule ^meetings$ http://blog.example.com/?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): blog.example.com/?page_id=2809
Is there a way to get the rule to work consistently for the subdomain and the subdirectory?
You may try this in one .htaccess file in /blog directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /blog
RewriteCond %{HTTP_HOST} blog\.example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /?page_id=2809 [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /blog/?page_id=2809 [L,NC]
For permanent redirection, replace [L,NC] with [R=301,L,NC].

Redirect non-www to www with SEO-friendly query strings

I am trying to redirect my site from non-www to www. My site is at [http://www.alennuskoodit.us]. I try to make it so that all requests without www would be redirected to www. Normal stuff so far.
However, if I go to http://alennuskoodit.us I end up here: http://www.alennuskoodit.us/index.php?qstr=http://www.alennuskoodit.us
This is the .htaccess:
Options +FollowSymLinks
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
# going to install folder
RewriteCond %{REQUEST_URI} (.*)/install/?$
RewriteRule ^(.*)$ %1/install/index.php [NE,R,L]
# going to Admin folder
RewriteCond %{REQUEST_URI} (.*)/admin/?$
RewriteRule ^(.*)$ %1/Admin/index.php [NE,R,L]
# working with client side
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
</IfModule>
This is what I tried, which doesn't work:
RewriteCond %{HTTP_HOST} ^alennuskoodit.us [NC]
RewriteRule ^(.*)$ http://www.alennuskoodit.us/$1 [R=301,NC,L]
How could I redirect all queries to http://alennuskoodit.us to http://www.alennuskoodit.us so that I would not end up breaking the other rewrites?
Place your new rule before all the other rules i.e
Options +FollowSymLinks
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{HTTP_HOST} ^alennuskoodit.us$ [NC]
RewriteRule ^(.*)$ http://www.alennuskoodit.us/$1 [R=301,NC,L]
#other rules here
and it should prevent the qstr= param
Step1= You Must Use This Code In htaccess File To Preferred www Version:
RewriteCond %{HTTP_HOST} !^(.).YourDomain.com$ [NC] RewriteRule ^(.)$ http://www.YourDomain.com/$1 [R=301,L]
Step2= You Must Setting Your Preferred Domain In Google WebMaster Tools :
Open up your webmaster tools and click “Settings” right below “Configuration”. To the right look for the “Preferred Domain” and select which domain you prefer With www Or Not.

Resources