.htaccess: 301 redirect not working in .htaccess in rewritten root path - apache

I have a domain called example.com which points to /public_html/ on my server.
In /public_html/.htaccess I have the following code to change the root path for my domain example.com to /public_html/example_path/:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L]
This is working very well.
Now, I want to redirect example.com/test.png to example.com/images/test.png. To do this, I'm using the following code in /public_html/example_path/.htaccess:
RewriteEngine On
Redirect 301 /test.png /images/test.png
Unfortunately, this isn't working, there's no redirection. The redirect is only working if I put the code in /public_html/.htaccess but not in /public_html/example_path/.htaccess.
Why that?

Inside your /public_html/.htaccess have redirect rule before example_path rule:
RewriteEngine On
RewriteRule ^test\.png$ /images/test.png [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L,NC]
Note that you cannot have image redirection rule inside /example_path/.htaccess because your image URL doesn't have /example_path/.

Related

Redirect root of website only with apache and .htaccess

I am looking to redirect only the root of my domain to another domain with apache + .htaccess - However I would like to maintain current directories.
Example:
http://www.domain-one.com/ would redirect to http://www.domain-two.com/
However anything after the / would be ignored, examples:
http://www.domain-one.com/contact
http://www.domain-one.com/?param=1
You can use this rule in the site root .htaccess of domain-one:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain-one\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?$ http://www.domain-two.com/ [L,R=301]

Redirecting subdomain to subdirectory through htaccess

I've been trying to redirect a subdomain to a subdirectory through htacess..
I have a domain lets say sub.domain.com and the directory domain.com/site/
I want to redirect sub.domain.com to domain.com/site not changing any url, simply redirecting in a SEO friendly way.
I've tried a redirect 301 rule but it doesn't seem to have worked.
Try this in your .htaccess:
RedirectMatch 301 wiki.comp.tf(.*) comp.tf/wiki$1
If that does not work, an alternative option is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wiki.comp.tf
RewriteRule ^(.*)$ http://comp.tf/wiki$1 [L,NC,QSA]
Some potential options for you using actual names:
Redirect 301 wiki.comp.tf comp.tf/wiki
You can use the following rule :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ /site/$1 [L]
This will internally redirect
sub.domain.com
to
/site/

.htaccess redirect chain generated by multiple redirects

Wondering if you can help me?
I'm working on a site at the moment, and the following is in the .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule ^$ http://www.example/folder-name [R=301,L]
As I see it, this works in the following manner:
root domain rewrites using 301 to www. and then an additional 301 redirect to the /folder-name.
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
NB. there is a subdomain, let's call that products.example.co.uk - and access would be needed here.
Is this possible? my .htaccess skills are not particularly tight so any help gratefully received!
This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/folder-name/$1 [L,R=301]
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
This should do it :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk [NC]
RewriteRule ^((?!folder).*)$ http://www.example.co.uk/folder/$1 [L,R=301]

htaccess redirect root only without changing url

i have 2 sites:
www.test1.com
and
www.test2.com
With the below code i can redirect from test1.com to test2.com without changing the url
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1.com
RewriteRule ^(.*) http://test2.com/$1 [P]
But if i try to access a folder in test1 for example test1.com/folder it also redirects.
So how i can only redirect the root folder but still without changing the url but be able to access a subfolder in my main domain?
Just change your regex to allow only landing page:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1\.com$ [NC]
RewriteRule ^/?$ http://test2.com [P]

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.