Apache mod_rewrite Redirect Alternative - apache

I am using:
Redirect 301 / http://newdomain.com
Problem is that it appends the end of the first to the new domain.
I need an alternative to redirect all requests to the new domain with out appending what follows the /.

You can use mod_alias for that, the syntaxis will be like this:
Redirect /something http://www.example.com/new_something
If you need something more complex, mod_alias has more options and commands, check mod_alias on the official documentation, or this askapache post

Possibly something like this in your .htaccess?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://newexample.com/$1 [L,R=301]

Try this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.com$
RewriteRule ^(.*)$ http://www.newsite.com/ [L,R=301]

Assuming newdomain.com is pointing to a new IP/host. You can use RedirectMatch instead of Redirect for its regex capabilities:
RedirectMatch 302 ^ http://newdomain.com

Related

.htaccess 301 redirect sub domain query

I need to 301 redirect this:
/blog/?=31
to
/blog/
and
/blog/?page_id=2
to
/blog/
I've tried the obvious stuff:
Redirect 301 /blog/?p=31 /blog/
Redirect 301 /blog/?page_id=2 /blog/
And obviously it didn't work otherwise I wouldn't ask this question here, so don't state the obvious!
Redirect directive is provided by mod_alias. I'm not sure, but I think that it does not receive the query string parameter. Using mod_rewrite though, the rules would be:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=31|page_id=2$
RewriteRule ^/?blog/?$ /blog/? [R=301,L]
In order to handle redirects involving Query Strings, you will need to use a mod_rewrite RewriteCond %{QUERY_STRING} in your .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^blog http://example.com/blog? [NC,L,R=301]

How to use .htaccess to redirect / to subdirectory

I am really confused by the grammar of .htaccess, so I come to ask for some help.
I have a website, let's say, www.example.com.
I want to use .htaccess file to redirect www.example.com to www.example.com/newsite/ except for www.example.com/keepit/
I mean, like
example.com => example.com/newsite
example.com/blabla/bla => example.com/newsite/blabla/bla
But example.com/keepit/blabla stays the same as example.com/keepit/blabla
I try to use .htaccess like this
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/keepit/$
RewriteRule ^(.*)$ /newsite/$1 [R=301,L]
It work to change the / to /newsite, but it failed in the need of omitting /keepit
Your regex is only matching /keepit/ not /keepit/anything.
You can use this rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(keepit|newsite) [NC]
RewriteRule ^(.*)$ /newsite/$1 [R=301,L]

Redirect m.example.com to m.example.com/subdir

how do i redirect m.example.com to m.example.com/subdir
i am using the following code for the same but it is getting cyclic and not redirecting properly
RewriteEngine on
RewriteCond %{HTTP_HOST} ^m.example.com$ [NC]
RewriteRule (.*)$ http://m.example.com/subdir [R=301,L]
I cannot also use:
redirect 301 / http://m.example.com/subdir
as my main www site will also get redirected
You need to add a condition so it doesn't redirect if the request is already for /subdir/. Add this somewhere above your rewrite rule:
RewriteCond %{REQUEST_URI} !^/subdir
You're also missing a backreference in your rule:
RewriteRule (.*)$ http://m.example.com/subdir/$1 [R=301,L]
If the only thing you want to redirect is requests for the root (URI=/), then you don't need the extra rewrite condition, only change your regular expression in your rule:
RewriteRule ^$ http://m.example.com/subdir [R=301,L]
To redirect just / but not anything under it you can use something like this in your m.example.com <VirtualHost>
RedirectMatch ^/$ http://m.example.com/subdir/
Alternatively, if you want to redirect everything into subdir except requests for subdir itself you could use mod_rewrite
RewriteCond %{REQUEST_URI} !^/subdir
RewriteRule ^(.*)$ http://m.example.com/subdir$1 [R=301,L]

htaccess rewrite base url

Anyone out there good with htaccess and mod rewrite - i need your help!
I need to rewrite the base part of a url.
for example all requestst to http://domain1.com need to go to http://domain2.com
The requests will typically be in the form as follows:
http://domain1.com/main/test?q=1
i then need to go to http://domain2.com/main/test?q=2
Pleas help!
Thanks in advance
Try this in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
# redirect for http
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^/?(.*)$ http://domain2.com/$1 [R=301,QSA,L,NE]
# redirect for https
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/?(.*)$ https://domain2.com/$1 [R=301,QSA,L,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters
$1 is your REQUEST_URI
Rewriting URL's across multiple domains? I'm not entirely sure this can work, considering Apache's request-handling algorithm. You're looking for a redirect, not rewrite.

Apache mod_rewrite: redirecting subdomains to the HTTPS login page

I'm trying to accomplish this:
http://subdomain.server.com/ should redirect to https://secure.server.com/login?subdomain, but http://subdomain.server.com/any-other-page should pass through as-is.
Can I accomplish this in the .htaccess file?
Try this rule:
RewriteCond %{HTTP_HOST} !=secure
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^$ https://secure.example.com/login?%1
RedirectMatch ^/?$ https://secure.server.com/login?subdomain