.htaccess 301 redirect sub domain query - apache

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]

Related

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/

Apache mod_rewrite Redirect Alternative

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

How can I make htaccess Dynamic and Static rules?

I have this dynamic rule:
RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]
and I am trying to add some static rules like this:
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools
The problem is when I type the following in the address bar:
http://www.{samedomain}.com/construction/pools.html
then apache redirects to:
http://www.{samedomain}.com/construction-services/pools?sub=construction&second=pools.html
What I want is apache redirects to:
http://www.{samedomain}.com/construction-services/pools
Does anybody know why?
Thank you.
Redirects are processed in the order they appear, so it should work to place the static redirect before the RewriteRule. Don't for get the [L] flag on your RewriteRule.
RewriteEngine On
# Match the static redirect first
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools
# Since your dynamic URLs don't end in .html, avoid those with RewriteCond
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^([^/]+)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC,L]
Or you could do it without the RewriteCond if none of your dynamic urls have a .
RewriteRule ^([^/]+)/([^.]+)$ /rewrite.php?sub=$1&second=$2 [NC,L]
Write the htaccess rule based on priority ( place the rule which has common behavior at last)
In your case
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools
RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]

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.