.htaccess redirect root url exclude index.php - apache

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.

Related

URL rewrite with HTTPS / www not working for subpages

I have to following configuration in my .htaccess. The first rule puts https before the URL and the second puts www before the URL, if not set already.
# https redirect
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# www redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
These redirects work perfect for the homepage. However, if you call a subpage, these rules wont work.
domain.xy -> https://www.domain.xy (works, Homepage)
domain.xy/contact -> http://domain.xy/contact (doesnt work)
The weird thing is, the favicon get redirected correctly. Example with the contact page, as seen on
this picture.
How can it be, that my configuration only works for the toplevel, not for any subpage?
I'd say your RewriteRule is wrong. It should be .* instead of ^:
# https redirect
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# www redirect
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The problem was another rule in the .htaccess-File. Our hosting provider automatically generates a .htaccess suiting the Typo3 installation. I had to place my rewrite rule before the following lines:
# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]
This rules, as far as i understand, redirects any request, which is not a file, directory or symlink back to the index.php. So in my case if i call domain.xy/contact, it redirects back to the index.php.
Because this rule has the [L] flag at the end, the .htaccess stops processing here. So i moved both my rules above this block and they get called before this block comes to action.

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

Apache rewrite sub2.domain.com/sub1/sub2 to sub2.domain.com

How to rewrite so that the subfolders go away in the browser URL?
We have a new subdomain that is already in DNS going to a subfolder:
Entering sub2.domain.com changes the url in the browser to sub2.domain.com/sub1/sub2.
RewriteCond %{HTTP_HOST} ^sub2\.domain.com$
RewriteRule ^(/)?$ sub1/sub2 [L]
I thought it's supposed to redirect first then rewrite? But this doesn't work:
RedirectMatch 301 ^/sub1/sub2/.*$ http://sub2.domain.com/
RewriteCond %{HTTP_HOST} ^sub2\.domain.com/sub1/sub2$
RewriteRule ^(.*)$ http://sub2\.domain\.com [L]

.htaccess redirect root to index.php

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]

URL Canonicalization - Apache mod_rewrite

I am probably attempting to take URL Canonicalization a bit too far but here it goes anyway.
Basically I am looking to the following:
301 redirect every base url to http://www.mydomain.com/ some pages are https it should recognize that and continue to use https where already used/requested
301 redirect away any trailing slashes ie http://www.mydomain.com/page/ becomes http://www.mydomain.com/page (I already have a line of code that finds the index.php page - this site is built on Codeigniter)
I Don't want the base url to have the slash stripped that is the only time a slash should be left behind
Find any instances of index.php (in the front middle or end of the url) and 301 redirect them out
ie http://www.mydomain.com/index.php/page/index.php/ becomes http://www.mydomain.com/page
301 redirect any use of my ip address to the actual domain
ie 11.11.111.111/page/index.php would become http://www.mydomain.com/page
Here is what I have so far in my htaccess file in my root directory:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} ^(.*)(/index\.php)$
RewriteRule ^(.*)index\.php/$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(mydomain\.com)(:80)? [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^11\.11\.111\.111$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#This makes sure that the server actually finds the index file although its not in the url
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index\.php/$1 [L]
I am stuck right now any help would be greatly appreciated!!
Revisions!!
I have made some progress and here is what I have so far
<IfModule mod_rewrite.c>
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# index.php to / at the base url
RewriteCond %{THE_REQUEST} ^GET\ /index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# force www.
rewritecond %{HTTP_HOST} ^paolienvelope.com [nc]
rewriterule ^(.*)$ http://www.paolienvelope.com/$1 [r=301,L]
# force no IP
RewriteCond %{HTTP_HOST} ^70.40.204.154
RewriteRule ^(.*) http://www.paolienvelope.com/$1 [r=301,L]
#codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
This successfully forces ip to url
Removes index.php or index.html from the url but correctly directs to the index file despite
makes sure the base url has www.
Still dont have the code to remove the trailing slash from only the request
any help would be appreciated!! Thanks!
RewriteEngine on
# index.php remove any index.php parts
RewriteCond %{THE_REQUEST} /index\.(php|html)
RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]
# force www. (also does the IP thing)
RewriteCond %{HTTP_HOST} !^www\.paolienvelope\.com [NC]
RewriteRule ^(.*)$ http://www.paolienvelope.com/$1 [R=301,L]
# remove tailing slash
DirectorySlash off
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js)
RewriteRule ^(.*)/$ $1 [R=301,L]
# codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Let's try to solve this one point at the time. As said I'm no pro at this yet but any bit helps i guess:
I'll start with 2. because that one seems easier:
#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.\.com$ [NC]
RewriteRule ^(.+)/$ http://www.mydomain.com/$1 [R=301,L]
Does this work, instead of what you have?
source:
http://blog.valtersboze.com/2009/06/add-or-remove-trailing-slash-in-url-with-htaccess/