301 redirect (.htaccess) of entire domain to another domain index - apache

I have tried solving this on my own but I can't seem to figure it out.
Here is what I'm trying to do:
Create a 301 redirect of all URLs (including index) on subdomain.example.com to example.com.
So for example:
subdomain.example.com redirects to example.com
subdomain.example.com/blog-post redirects to example.com
This means that I DO NOT want to keep the same URL structure that's used on subdomain.example.com. I just want URLs on the subdomain to point to the index on the example.com. I know that's not the best solution for SEO, but that's how I want it.
I have tried accomplish this using
Redirect 301 / https://example.com/
but that keeps the URL structure and I don't want that.
So how can I solve this?

according to the documentation https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
keeps the part of the url.
With redirectMatch you can use regexes and substitution to rewrite your url
e.g.
RedirectMatch 301 . https://example.com/
where the dot (.) is a match anything.

You can not achieve this with a Redirect as it appends the old URL path to new one. You can use RewriteRule
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule (.*) https://example.com/ [R=301,L]

Related

redirect exact url to another url

There are a bazillion examples online of doing redirects via apache's htaccess, but I can't find any example of redirecting a full URL match to another full URL.
For instance, I have an existing website at
https://example.com
How do I redirect some specific URLs for that domain to a different one:
https://example.com/login --> https://my.example.com/login
https://example.com/register --> https://my.example.com/register
For every other every other path on example.com I need to remain untouched so my site still works fine (i.e. /blog shouldn't redirect somewhere else).
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(login|register)/?$ http://my.example.com/$1 [L,R]
This will redirect example.com/login or example.com/register to http://my.example.com/
You can alternatively accomplish this using the RedirectMatch directive (if the newurl is on a diffrent webserver) :
RedirectMatch ^/(login|register)/?$ http://my.example.com/$1

Redirect all request from old domain to new domain

I am looking to migrate from old domain to new domain.
I have my old domain olddomain.com and new domain newdomain.com pointing to same ip address for now.
I have Apache server inplace to handle requests.
How do I 301 redirect all my
olddomain.com/*
&
www.olddomain.com/*
to
newdomain.com/*
Can I get exact regex or configuration that I need to add in htaccess.
My newdomain.com and olddomain.com both are being serverd by same apache from same IP so "/" redirect might lead to cycles? And so was looking for effecient way
I tried
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
# RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://comp16/$1 [R=301,L]
</IfModule>
And even tried adding in virtual host
RedirectMatch (.*)\.jpg$ http://comp17$1.jpg
But it does not redirect site when i hit localhost in browser to my computer name i.e comp16
In the configuration (VirtualHost) for each of your olddomain.com host try this:
Redirect permanent / http://newdomain.com/
Apache documentation for Redirect. This is the preferred way when everything should be redirected. If you must use mode_rewrite/htaccess there are plenty of questions around this on SO and one of them is:
How do I 301 redirect one domain to the other if the first has a folder path
EDIT
Recommendation from Apache regarding simple redirects:
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to
redirect one URL to another. This kind of simple redirection of one URL, or a class of
URLs, to somewhere else, should be accomplished using these directives rather than
RewriteRule. RedirectMatch allows you to include a regular expression in your
redirection criteria, providing many of the benefits of using RewriteRule.
I also recommend to use an If statement as you can use it also in a multisite server. Just type:
<If "%{HTTP_HOST} == 'old.example.com'">
Redirect "/" "https://new.example.com/"
</If>
Write the below code in to your .htaccess and it will redirect all your old domain request to new domain.
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]

Simple 301 redirect in .htaccess with query string does not work with Redirect directive

I am trying to redirect a single URL in a .htaccess file with Redirect:
Redirect 301 /index2.php?option=com_rss&feed=RSS2.0&no_html=1 /something/somethingelse/
I have a bunch of other similar rules which work using directory structure URLs, but this one refuses to get processed.
Redirect 301 /old/url/ /new/url/
Do I have to do anything special?
Thanks!
With Redirect you can only test for URL paths, or more specifically, URL path prefixes but not for the URL query. But you can do so with mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} =option=com_rss&feed=RSS2.0&no_html=1
RewriteRule ^index2\.php$ /something/somethingelse/? [L,R=301]

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.

Apache redirect

I would like to redirect a URL using RedirectMatch within Apache eg,
/test/one/?? redirect to /test/two/??
where the ?? represents any string that follows
The redirect i'm using below does a straight redirect but doesnt match any string after...
RedirectMatch Permanent ^/test/one?$ /test/two/
Thanks alot
RewriteEngine ON
RewriteBase /
RewriteRule ^/test/one/(.+)$ /test/two/$1
if that does not work, change ^/test/one into ^test/one
make sure mod_rewrite is enabled
You can use mod_rewrite for this:
RewriteEngine On
RewriteBase /
RewriteRule ^/test/one/(.*) /test/two/$1 [L,R=301]
The R flag redirects the page rather than internally rewriting the URI. 301 is the HTTP status code for "Permanently Moved" - if you'd rather use another, you can change it to one of these.