.htaccess redirect root to index.php - apache

I need to redirect from http://example.com/ to http://example.com/index.php.

Use this:
DirectoryIndex index.php

Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]

Just to add my own solution, since the answer of Michael did not work for me, I did something like:
RewriteEngine on
# These two lines redirect non-www to www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com$1 [R=301,L]
# These two lines redirect the root to index.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]
This also preserves any possible existing query string parameters.

Adding to #Uwe Keim's answer above, I had non-www to www setup on Apache virtual hosts.
What worked for me on .htaccess was:
RewriteEngine on
# Two lines to redirect from the root web directory to myfile.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]

Related

how to 301 redirect from root domain to a page with htaccess

i am trying to auto 301 redirect from domain.com to domain.com/v2
ive tried this code
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^$ /v2/ [NC,L]
also tried
Redirect 301 / /v2/
i get the error 'site has tried to redirect you too many times'
is there any way to do this?
thanks for any help
Untested but this should redirect both domain.com and www.domain.com to the v2 subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
You need to replace example.com with your actual domain because of posting restrictions.
I suppose you have an endless redirect because you are not matching the end of the domain string with $. Redirection should only take place at the root domain.
you can use a write rule with ^$ representing the root of the site and rewrite it to the selected folder
RedirectMatch ^/$ /v2/
it redirects the root and only the root URL.
or also you can use
RewriteEngine On
RewriteRule ^$ /v2[L]
If you want external redirection(301) you can use R flag
RewriteRule ^$ /v2[L,R=301]
Another way to use it with RewriteEngine
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
Original Answers
I hope I've helped

.htaccess redirect root url exclude index.php

I want to redirect the root URL only excluding index.php which is in root folder. This is what i have so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} website\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://website.com/redirect/ [L,R=301]
RewriteRule ^/index\.php$ - [L]
It still redirects the index.php file when manually entered!
Please try the following:
RewriteEngine on
# Condition: Only match host 'website.com'
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
# Rule: If request is for root, redirect
RewriteRule ^$ http://website.com/redirect/ [R=302,L]
As you used 301 already, you may need to clear the cache for the browser to redirect properly.
Once you are happy, and would like to make the redirect permanent, change 302 to 301.

Rewrite to subdomain and rewrite again there

I have / and /sub on my webspace.
The subdomain sub.example.com takes his data from /sub.
In / there is a Joomla installation and its .htaccess.
As first I want to redirect all access on /sub to sub.example.com and as second all access on sub.example.com to sub.example.com/index.html due maintenance.
I can redirect all access on /sub to sub.example.com via
RewriteEngine on
RewriteRule ^sub(.*)? http://sub.example.com$1 [R=301,L]
But when I enable the following rule in /sub/.htaccess the rule above fails.
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.html
RewriteRule .* /index.html
I know that's because of the second rewrite in /sub/.htaccess, but how can I do the rewrite above?
I tried the following rules in the /.htaccess but it does not work
RewriteEngine On
RewriteRule ^sub(.*)? http://sub.example.com$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteCond %{REQUEST_URI} !index\.html
RewriteRule .* index.html [L]
Place this in /sub/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^sub\.example\.com$ [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule !^index\.html index.html [L,NC]

.htaccess RewriteRule redirect

i need help redirecting this two links to one link:
Link #1: http://www.domain.com/index.php?til=d_news&id_new=1
Link #2: http://www.domain.com/new_folder/?til=d_news&id_new=1
need to redirect to:
http://www.domain.com/news/news.html
i tried using this code:
RewriteCond %{QUERY_STRING} ^til=d_news
RewriteRule ^(.*)$ http://%{HTTP_HOST}/news/news.html? [R=301,L]
but it only redirects the first link and not the second.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1 [NC]
RewriteRule ^ /news/news.html? [R=301,L]
In the htaccess file in your document root, add:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1
RewriteRule ^index.php$ /news/news.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1
RewriteRule ^new_folder/?$ /news/news.html? [L,R=301]
In your second link there is no destination page:
http://www.domain.com/new_folder/?til=d_news&id_new=1
should be something like:
http://www.domain.com/new_folder/page.php?til=d_news&id_new=1

How can I use mod_rewrite to 301 redirect example.com to www.example.com?

I need to redirect any URLs without "www." to URLs with "www." for better search engine optimization. I read that this is possible with mod_rewrite and .htaccess files, but I do not know the right code to use. Can anyone help?
Create a file called .htaccess in your root folder (the one where, say, index.html or index.php resides). Put the following into it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
There is an excellent example of this in Apache's URL Rewriting Guide.
The following code would redirect any non-www request to a www request:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
You'd want to put this inside the <Directory> directive of your .htaccess file, or apache config file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
To remove www from your url website use this code on .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
To force www in your website url use this code on .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
Were "YourSite.com" you must replace for your url.
In addition to using mob_rewrite you can do this with a virtual host directive
<VirtualHost example.com>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
I usually do it the other way around to remove the extraneous 'www'.