Redirect documentroot to another folder in conf apache or .htccess - apache

Good Day
I want to rewrite a url when request a subdirectory
When the user request
172.0.0.1/url
i want to rewrite to make this url point to
172.0.0.1/url/url
or the documentroot is in /var/www/html/url/url make this point to
172.0.0.1/url

The trick here is to avoid creating a rewrite and a redirect loop. You can put this code in your root folder htaccess
RewriteEngine On
# Redirect /url/url/xxx to /url/xxx
RewriteCond %{THE_REQUEST} \s/(url)/url/(.*)\s [NC]
RewriteRule ^ /%1/%2 [R=301,L]
# Internally rewrite back /url/xxx to /url/url/xxx
RewriteRule ^(url)/((?!url/).*)$ /$1/$1/$2 [L]

You can use .htaccess to redirect /url to /url/url
Just Create .htaccess file at /var/www/html/ with following contains:
RewriteEngine on
RewriteRule ^/?url/(.*)$ /url/url/$1 [R,L]
This will Redirect all incoming requests at /url and redirect them to /url/url
But you cannot redirect requests coming at /url/url to /url Because we are already redirecting request at /url, this will result in catch 22 and browser will keep redirecting back and forth between /url and /url/url.

Related

Redirect all path except a few in htaccess

Id like to use the .htaccess file on an apache servier redirect all paths on a given domain to a new location but keep a few specific URLs live.
Redirect 301 /(.*) www.example.com
# but not /foo
# but not /bar
# but not /baz
You cannot do it using Redirect directive. I suggest using mod_rewrite:
RewriteEngine On
RewriteCond %{THE_REQUEST} !/(?:foo|bar|baz)[?/\s] [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,NE,R=301]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

.htaccess redirect folder to its subdomain causes redirect loop

I have a subdomain sub.domain.com
And the same contents are available on domain.com/sub
But now I want to redirect domain.com/sub to sub.domain.com
If I do that with
Redirect 301 /sub http://sub.domain.com
it causes a redirect loop.
How can this be solved technically? Users should not be able to access the subdomain content under domain.com/sub but should be redirected.
Better to use mod_rewrite rules here with conditions to restrict the rule only for domain.com requests:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*)?$ http://sub.domain.com/$1 [L,NC,R=302]
Make sure to test this after clearing browser cache.

redirect download directory to subdomain

my download script structure is:
http://www.mysite.com/download/viewdownload/**/**
now i want move and redirect to subdomain
for example
redirect
http://www.mysite.com/download/viewdownload/61/4249
to
http://www.alt.mysite.com/download/viewdownload/61/4249
or
http://www.mysite.com/download/viewdownload/53/824
to
http://www.alt.mysite.com/download/viewdownload/53/824
You can use either mod_alias:
Redirect /download/viewdownload http://www.alt.mysite.com/download/viewdownload
In the vhost config or htaccess in the document root of the www.mysite.com host, or use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^/?download/viewdownload/(.*)$ http://www.alt.mysite.com/download/viewdownload/$1 [L,R]

Redirect traffic to CDN page

So anytime someone visits this page:
http://www.site.com/page/image_(.*).png
They get redirected to the cdn url, which would be
http://cdn.site.com/page/image_(.*).png
How can I do that using htaccess? I only need this for that url.
Using mod_alias, if you have access to vhost config, in your main vhost (for www) add:
RedirectMatch 301 ^/page/image_(.*)\.png http://cdn.site.com/page/image_$1.png
If you don't have access and have to use an .htaccess file, you need mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site.com [NC]
RewriteRule ^page/image_(.*).png http://cdn.site.com/page/image_$1.png [R=301,L]

Need help configuring 301 permanent redirect in Apache for non www

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks
Add the following:
# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.boom.com/$1 [L,R=301]
This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).
If you have named virtual hosts you could put the extra RewriteCond entries #tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:
<VirtualHost boom.com:80>
RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>
I'm sure someone will comment if there's a reason to use one over the other.