htaccess redirect root to www - .htaccess

This question seems to be asked a lot before, but I'll ask it again, because it doesn't seem specific for my problem:
I want the domain.com, to be directed to www.domain.com
So I use this:
RewriteEngine On
Options +Indexes +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
It just doesn't seem to work.
Now I'm hoping and thinking the problem is with my hoster, but they are not particularly responsive. When I go to the root domain I get a server error 500. When I contacted them they said I had to use a .htaccess file, but I think the problem is the HOSTS file, which does actually point to the right location (the root domain A-record points to the right IP, and the www. points to the root domain, so I'm thouroughly confused...)

Try adding this at the top. The extension of index file is dependant on your programming language.
DirectoryIndex index.html index.php
Options +Indexes +FollowSymLinks -MultiViews

Related

.htaccess specific url redirect to another domain url [duplicate]

After looking on the internet for about an hour, I didn't find the answer to my question. So I'm searching with the wrong keywords or what I want is not possible.
What I want:
I have multiple domains with different extensions, for example:
mydomain.be
mydomain.nl
Now what I want is that the mydomain.be is redirected to mydomain.nl. The solution for this I have found on the internet and shown below, with the need of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.be$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.be$
RewriteRule (.*)$ http://www.mydomain.nl/$1 [R=301,L]
With this code, when you type mydomain.be you will be redirect to mydomain.nl. But also the URL in the addressbar is changed to mydomain.nl. What I want is to keep the URL in the addressbar mydomain.be.
So, mydomain.be:
keep URL
show content of mydomain.nl
How To?
It is possible to get it done via mod_rewrite but make sure mod_proxy is enabled in your Apache's httpd.conf. Once that is done enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.be$ [NC]
RewriteRule ^ http://www.mydomain.nl%{REQUEST_URI} [L,NE,P]
Take note of flag P which is used for handling the proxy request.
Read more about flag: P in mod_rewrite
Another option without hassling with .htaccess would be to point both domains to the same document root or setting one domain as an alias for the other, depending on how you are able to configure your Apache. However, this has downsides:
If your content management system uses absolute URLs a user who clicks on mydomain.nl on a link will be directed to the mydomain.be domain (WordPress does this, as an example).
Search engines punish this behaviour by placing you further down on the search results. at least Google does, they have an interesting blog post about duplicate content. Not sure about competitors.
An example apache config could be:
<VirtualHost *:80>
ServerName mydomain.nl
ServerAlias mydomain.be
DocumentRoot /var/www/mydomain.nl/htdocs
</VirtualHost>

301 Redirect entire site to new directory except for index.html landing page

The wordpress site I'm working on got moved into a subdirectory therefore all the links from other sites don't work anymore. I implemented a 301 redirect with .htaccess which is great because it fixes that problem BUT the old root directory now has an index.html that has landing page my client absolutely wants to be seen.
So, how can I set up my .htaccess to redirect all traffic into the sub directory (to fix the incoming links) EXCEPT the index.html in the root directory because it has the landing page.
I don't know how htaccess works well but this is what I have right now.
Order deny,allow
ErrorDocument 404 /404.php
DirectoryIndex index.php index.html
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$ [OR]
RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]
Thanks!!
Edit for clarification:
Right now EVERYTHING redirects from the root to the subdirectory. I want everything to redirect except for index.html in the root directory. If the user requests just the domain name (http://example.com) without specifying a page, I also want him/her to be served up the index.html page in the root directory.
The following code does what you are asking for: "if the request does not match either index.php or index.html or "/" (i.e. nothing) (and the match is not case sensitive) then serve up the alternate location"
Order deny,allow
ErrorDocument 404 /404.php
DirectoryIndex index.php index.html
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.(php|html) [NC]
RewriteCond %{REQUEST_URI} !^/$ {NC]
RewriteRule ^.*$ http://example.com/portal/$0 [R=301,L]
I have tested this using the excellent online testing tool http://htaccess.madewithlove.be
Using the following test cases:
http://example.com -- no rewrite, second condition not met
http://example.com/ -- ditto
http://example.com/index.html -- first condition not met
http://example.com/index.php -- first condition not met
http://example.com/some/page.html -- rewritten as http://example.com/portal/some/page.html
EDIT You said that this still didn't work quite as expected; so I brought out the big guns. By turning on "maximum logging" of everything that the rewrite engine does with the directives
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 9
(pick any path you want, obviously), then looking at the end of the logfile in a terminal window with
tail -f /var/log/apache2/rewrite.log
You can quickly see where things are not working quite right. A bit of fiddling led me to the following code. It says "if the requested URI is just /index.html or /index.php, or if it starts with /portal, or if it is blank, then don't redirect.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.(php|html) [NC]
RewriteCond %{REQUEST_URI} !^/portal.*$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule ^(.*)$ http://example.com/portal$0 [R=301,L]
The test cases worked for me - see if they work for you!
NOTE: I made these changes in the httpd.conf file, not in the .htaccess file of the root directory. You need to be careful to make it so the .htaccess file in the root directory is even read - the default Apache configuration has an Override none for that directory, so some extra work is needed. By putting this configuration change in the httpd.conf file (and issuing a sudo apachectl restart command) you avoid the difficulty. Depending on who is hosting your website, and what control you have, that may not be an option for you. There may come a point where the experts for this problem can be found on superuser.com rather than SO... but I'm hopeful this does the trick for you.

URL-rewriting with index in a "public" folder

I’m a newcomer in the development world. I desperately try to get the good URL. I checked the site for similar problems but I can’t find exactly what I need. Or I do it badly.
Here is the situation: I set up a project for a site whose the index.php file is in a folder named Public.
To be clearer, here is the URL I have now to reach the homepage of the built site:
http:// Domain Name.com/ Folder / Name of the site/public
My concern is about the folder Public: I don’t want it appears in the URL.
Here is the URL I’d like to get:
http:// Domain Name.com/ Folder / Name of the site
In fact, I’d like this URL permits to get the index file placed in the folder "Public".
I can’t access the Apache configurations (shared host) so I have a .htaccess I placed in the project (i.e: www/ Folder /Name of the site /.htaccess). Here is its content:
Options -Indexes +FollowSymLinks +Multiviews
RewriteEngine On
RewriteBase /public/
RewriteRule ^(.*)$ index.html [NC,L]
I made something very simple for now because I tried lots of things without efficient result.
Not really sure what you are trying to do, but if you want to remove the /public/ path that appears in the URL, you need to remove it from all your links, second, turn off multiviews, it's not what you want, third, you need a rule to externally redirect the browser when a request is made for /public/, then you need to internally rewrite requests to point to public.
Options -Indexes +FollowSymLinks -Multiviews
RewriteEngine On
# externally redirect, must match against %{THE_REQUEST}
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /public/
RewriteRule ^/?public/(.*)$ /$1 [L,R=301]
# internally rewrite it back, but we must first check that it's pointing to a valid resource:
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -s
RewriteRule ^(.*)$ /public/$1 [L]

Remove folder with HTACCESS

When users go to the following url:
http://mysite.com/folder/subfolder?a=blah&b=blah
I need to point them to content that is in the root folder while keeping the variables intact:
http://mysite.com/?a=blah&b=blah
Since http://mysite.com is a virtual host, this solution should also leave other base URLs alone.
I'd prefer not to do a full redirect because we use log tracking in our system and we need to have the headers return a 200 code for the logs (maybe this doesn't matter).
I know that this is a fairly straightforward question for someone who really understands .htaccess redirects. Thanks in advance!
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 %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^folder/subfolder/?$ / [L,NC]

Can you do a redirect in .htaccess, and preserve the URI?

Is there a way to use .htaccess to redirect example.net to example.com, so that the URI example.net stays the same in the client's browser?
I found these examples from this link:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domain2.com
RewriteCond %{REQUEST_URI} !subdirectory/
RewriteRule ^(.*)$ subdirectory/$1 [L]
or
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} domain2.com
RewriteCond %{REQUEST_URI} !mychatfolder/
RewriteRule ^(.*)$ http://www.domain1.com/subfolder/$1 [L]
I haven't tested these out though.
If I'm understanding your question correctly, what you want is two sites which appear exactly the same, but neither one redirects to the other. You want the person who visits example.net to see the same stuff as is on example.com, but they should still see example.net in their browser address bar.
There are a few ways of doing this. The simplest, of course, is to simply upload the same content to both sites, but this might make updating tricky. Another is to set one domain as an alias of the other; exactly how to do this will depend on your server configuration. It would be simpler, and would perhaps be better for SEO purposes, to redirect one domain to the other.
RewriteEngine On
RewriteRule ^(.*)$ http://www.example.com/$1 [R=302,L]
# 302 is a temporary redirect. Use it for testing purposes. Once you're sure it works, change it to 301.
If you want the domain example.net to point to example.com then the best way isn't rewriting it, but to park that domain to example.com and the app. for that is basically titled as Parked Domains that's in Domains section if your control panel is cPanel or if your web hosting account features an app. that can park a domain.

Resources