I've got a very simple .htaccess file that enables the rewrite engine, and lets me use URI's in my web applications.
The problem is i need to do a 301 redirect to get my webaddress without www redirected to my website WITH www (While keeping the current rewrite function that redirects every subdirectory access to index.php in the root of my domain), to optimize seo. I just can't get it to work. :(
So in short terms i need to combine the current code which is:
RewriteEngine on
RewriteRule ^(.+)/$ index.php
With the 301 rewrite rule which is
RewriteEngine on
RewriteCond %{HTTP:Host} ^website\.com$
RewriteRule (.*) http\://www.website.com$1 [NC,R=301]
My server is running Apache, with the mod rewrite enabled.
Thanks in advance
You don't make it entirely clear except in your example that your using php, however, using Helicon ISAPI Rewrite for IIS / .NET I use:
RewriteCond %{HTTP:Host} ^website\.com$
RewriteRule (.*) http\://www.website.com$1 [NC,R=301]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Reference: http://www.rlmseo.com/blog/htaccess-rewrite-www/
Related
My aim is to redirect my current old domain to my new domain. All the page structures are the very same, only thing that has changed is the domain.
I'm wanting to redirect the entire site to the new domain within my .htaccess file. What exact line of code will I use?
Are you using mod_alias Apache module?
Redirect 301 / http://www.new-domain.com/
Use the mod_rewrite Apache module
RewriteEngine On
RewriteRule ^(.*)$ http://new_domain.com/ [R=301]
Maybe:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^old_domain\.com$ [NC]
RewriteRule ^(.*)$ http://new_domain.com [R=301,L]
This is a good tutorial about your problem...
I'm currently using this code to redirect all request to index.php
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^([^.]+)/?$ index.php?get=$1 [L]
While having this universal code, how can I specify an individual redirect?
E.g.,
Redirect 301 /foo http://example.com/foo-bar
Place this above your current set of rules. The L flag will cause Apache to stop processing the rest of the .htaccess file. Also be sure to clear your browser cache.
RewriteRule ^foo$ /foo-bar [R=301,L]
A nice tool for testing .htaccess files can be found here http://htaccess.mwl.be/
I'm trying to do a permanent redirect with .htaccess, but it isn't working and I have no idea why.
RedirectPermanent / http://www.flunchinvite.fr
I'm trying to do a redirection from : http://www.flunchinvite.com to: http://www.flunchinvite.fr.
Do you have any ideas?
Thanks
edit
I've just did a test to do a redirect to google, and it doesn't work either, whereas when I try to do a redirect with the same code on http://flunchinvite.fr it works. Do you know where that can come from ?
Try something similar to
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^flunchinvite.com[nc]
RewriteRule ^(.*)$ http://www.flunchinvite.cfr/$1 [r=301,nc]
Use Rewrite if it is an option:
http://www.gnc-web-creations.com/301-redirect.htm
Another method we can use is via mod_rewrite. This requires that the
mod_rewrite module is active on your webserver. It usually is and is
done by the system administrators when they installed the webserver.
mod_rewrite is a very powerful URL re-writing engine and we will only
by scratching a hair on its head here.
Again, in your .htaccess file
RewriteEngine ON RewriteRule ^(.*)$ http://mynewdomain.com/$1
[R=301,L]
The above example will re-map your old domain to a new one and issue a
301 status code (permanent redirect). So a request for
http://olddomain.com/foobar.html will go to
http://mynewdomain.com/foobar.html
If you simply want to redirect all requests regardless of the page
requested to the new domain you could use:
RewriteRule /.* http://mynewdomain.com/ [R=301,L]
In this case no matter what file or directory is requested they will
all go to
http://mynewdomain.com/ i.e., http://myolddomain.com/foobar.html
will go to http://mynewdomain.com/
The [R=301,L] means redirect the client and send a 301 status code
(R=301) and make this the last rule (L).
At the end I did a php redirection, I don't know why it's not ok on the htaccess. I'll see that another time. I'm going to bed
Take a look at lines 5 and 6:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo2
RewriteCond %{HTTP_HOST} ^mathpdq\.com
RewriteRule ^(.*)$ http://www.mathpdq.com/demo2/$1 [R=permanent,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I could not get 301 redirects to work so I went with this. basically if the user goes in with mathpdq.com/demo2 it forces a redirect to www.mathpdq.com/demo2.
The stuff below line 6 is just the normal mapping into the php functions.
http://pastie.org/5364605
I've a domain like example.com. I want to all the traffic on it to be sent to sub.example.com.
Currently I'm using this code but it sends to example.com/sub:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond $1 !^sub
RewriteRule ^(.*)$ /sub/$1 [L]
Is there a way to do this?
I think you have to use the R flag: Forces an external redirect, optionally with the specified HTTP status code. details...
RewriteRule (.*) http://... [L,R]
Just put the following code in to your .htaccess file.
Redirect 301 / http://sub.domain.com
Remember to change http://sub.domain.com to your sub-domain.
Connor
I want write a rule in .htaccess for redirecting
http://www.dir.domain.com
http://dir.domain.com
http://www.domain.com/dir
these should redirect to
http://domain.com/dir
Thanks in advance...
The trick here is to use an .htaccess file in your DOCROOT
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?dir\.domain\.com$
RewriteRule ^.* http://domain.com/dir/$0 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^dir/.* http://domain.com/$0 [R=301,L]
Note that this is doing a 301 redirect -- that is the server tells the browser that this is a permanent redirect and the browser caches this redirect.
You can also do an internal redirect where this mapping is done silently at the server end by tweaking flags.
This assumes that your shared service is using Apache, if its using IIS, then you need to do something similar with the web.config
You can try:
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$
RewriteRule ^ http://domain.com/%1 [L,NE]
Please change domain by your domain, when you enter sub1.domain.com then it will redirect to domain.com/sub1, when you enter sub2.domain.com then it will redirect to domain.com/sub2 ....