Mod Rewrite allow one directory redirect all other traffic - apache

I have a domain which is currently redirecting all traffic to another domain but I want to set up a sub directory on the first domain and allow traffic to that sub directory while continuing to redirect all other traffic. The rewrite directives currently in use are as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^firstdomain.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.firstdomain.com.au$
RewriteRule ^(.*)$ "http\:\/\/www\.seconddomain\.com\/$1" [R=301,L]
I want to be able to access anything in a subdirectory www.firstdomain.com.au/wp/
Is there a simple solution? Thanks in advance.

Try this rule in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?firstdomain\.com\.au$ [NC]
RewriteCond %{REQUEST_URI} !^/wp/ [NC]
RewriteRule ^(.*)$ http://www.seconddomain.com/$1 [R=301,L]

Related

.htaccess redirect to another folder but keep the original domain

First,
my 'example.com' domain is linked to '/home/defaultfolder/' folder.
I want to redirect
example.com , www.example.com
to
/home/somefolder/example/ , not /home/defaultfolder/ folder
by using .htaccess. (both are inside the DocumentRoot directory)
also, subdomains like
a.example.com
to
/somefolder/example/a/
but keeps the 'example.com' domain.
I have tried some examples on the web, but nothing could have done it.
How can I write the .htaccess to do so? Thank you.
Of course, I cannot change server settings(like alias virtual hosts..) and that's why I am trying to do it by modifying the file.
I have tried
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule /somefolder/example/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule !^/somefolder/example/ /somefolder/example/1%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example.com
RewriteRule ^(.*)$ /somefolder/example/$1 [R=permanent,L]
and some others..
Have it this way in /home/.htaccess (site root):
RewriteEngine On
# handle example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example)\.com$ [NC]
RewriteRule .* somefolder/%1/$0 [L]
# handle any sub.example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.(example)\.com$ [NC]
RewriteRule .* somefolder/%2/%1/$0 [L]
I think you should modify the server setting, make the domain root folder to what you want.

.htaccess redirect request from specific domain to another domain

I have an apache server hosting a site. I have 2 domains pointing to this server, www.mysite.se and www.mysite.com.
I'm trying to figure out how to in the htaccess file to redirect traffic coming from the www.mysite.se domain to www.mysite.com/se/
I've tried several ways but cannot get it to work. I always just end up on the root of the site, as in www.mysite.com instead of the /se/ path.
This is what I've tried so far:
RewriteEngine On
RewriteCond %{http_host} ^www\.mysite\.se [NC]
RewriteRule ^(.*)$ http://www.mysite.com/se/ [R=301,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$ [NC]
RewriteRule ^(.*)$ /se/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule ^(/)?$ se [L]
What am I doing wrong?
This seems to have finally done it!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule (.*)$ http://www.mysite.com/se/ [R=301,L]

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

How to redirect a subfolder to a different domain?

I need to redirect a subfolder to another domain with the same subfolder name.
For example, I want to redirect the following url
www.domain.com/photo
...to another domain but same subfolder
www.domain2.net/photo
...using mod_rewrite in .htaccess.
Try these lines in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for HTTP
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.net/$1 [R=301,L]
# for HTTPS
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain2.net/$1 [R=301,L]
You don't need mod_rewrite to do this. In fact, avoiding it is simpler, more readable and thus more maintainable, and often faster.
Try this in domain.com's root .htaccess file (or, more efficiently, in the domain configuration file itself):
RedirectMatch ^/photo/(.+) http://domain2.com/$1

redirect to subfolder

I have set up url redirect on my host--but its not working.
my htaccess looks like this now
rewriteengine on
rewritecond %{HTTP_HOST} ^.*$
rewriterule ^http:\/\/www\.bangkoksoftball\.info "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L] #4d3a8d4534e567
what i want is any request to http://www.bangkoksoftball.info to be redirected to http://www.bangkoksoftball.info/wordpress/
but any request to a path file or directory off the root not to be redirected
this so people can still access the old site via /home.html or index.html etc.. and still be able to navigate outside the wordpress folder
Try this in your .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^(www\.)?bangkoksoftball\.info$ [NC]
RewriteRule ^/?$ /wordpress/ [R,L,NE]
Remember there is NO presence of http/https in %{HTTP_HOST} or %{REQUEST_URI} variables. Also . needs to be escaped in domain match. NE flag is for not escaping the query parameters.
The rewriterule line does not look at the domain, you must specify the domain the rule applies to in the RewriteCond line. In the rewriterule line, you specify first the paths that should trigger the rule, and the path the user should be sent to instead.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]
This will send only requests to www.bangkoksoftball.info/ to the wordpress folder. If you want to also redirect www.bangkoksoftball.info/index.php to the wordpress folder, you would need to add an additional set of directives:
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/index\.php$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]
This should work for you as it worked here:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?bangkoksoftball.info$ [NC]
RewriteRule ^/$ http://www.bangkoksoftball.info/wordpress/$1 [R=301,L]