I'm using the following code to redirect all requests to one domain to another single page (without stuff after the /)
RewriteEngine on
RewriteRule ^(.*)$ http://example.com [L,R=301]
The above works if I go to olddomain.com or olddomain.com/something, but doesn't work when going to olddomain.com/something/somethingelse
How can I get the rewrite rule to work for all directory depths?
Should have worked, You can try this code also:
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301]
Related
I have two different kinds of rules in my .htaccess file. The first group matches on exact files, and then I have a generic catch-all for everything else. I have read you can use the [L] for rewrite rules, but is there an equivalent for Redirect 301? For example, my .htaccess file looks like this:
Redirect 301 /exact_page.html http: //www.newsite.com/new_page1.html
Redirect 301 /some_other_page.html http ://www.newsite.com/new_page2.html
RewriteEngine on
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
What I would like is for pages exact_page.html and some_other_page.html to be redirected exactly as shown, and everything else gets maps from the domain to the new domain, with the rest of the url intact. Instead, it looks to me like the first two Redirect 301's are being ignored, or more precisely, are being superseded by the final rule. Is there a way to tell apache to stop after it finds the first match?
I think something like this should work:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(exact_page\.html|some_other_page\.html)$
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
RewriteRule ^exact_page\.html$ http://www.newsite.com/new_page1.html [R=301,L]
RewriteRule ^some_other_page\.html$ http://www.newsite.com/new_page2.html [R=301,L]
I have a domain like:
this.that.com
(it can also be accessed from:)
that.com/this
What I would like to do is make it so that the server transfers all requests from the two above url's to:
this.com
I need it so if I type in this.that.com
it will auto transfer to this.com
thanks in advance.
To redirect all requests to this.com, match everything, which is not already this.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/$0 [R,L]
If you want to redirect to the main page instead, leave out the URL path
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/ [R,L]
If everything works as expected, you can switch to R=301 redirection.
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 need to change the url of a directory so that:
www.example.com/foo/
becomes
www.example.com
I can't move the files.
Putting a htaccess file using mod_rewrite to simply rewrite www.example.com to www.example.com/foo wasn't a problem
RewriteEngine On
RewriteRule !^foo/ foo%{REQUEST_URI} [L]
However I must ensure that if the user requests www.example.com/foo that the http status is 404 or the user is redirected to www.example.com. Unfortunately whatever I do, it seems to end up in an infinite loop. For example this results in an infinite redirection loop:
RewriteEngine On
RewriteRule !^foo/ foo%{REQUEST_URI} [L]
RewriteCond %{REQUEST_URI} ^/foo
RewriteRule ^foo(.*) http://www.example.com$1 [R=301,L]
You need to condition on the original request sent to the server, since the %{REQUEST_URI} will change during the mod_rewrite processing, which causes the internal redirect loop.
Consequently, something like this should take care of things (for the 404, your 301 RewriteRule should work fine as well if you want to swap that in):
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/foo [NC]
RewriteRule ^ - [R=404]
So I have moved a website and am trying to 301 redirect everything, which I do quite often so this is a weird problem but probably something stupid I'm not seeing.
ALL of my redirects are working fine, except any redirect that the first string starts with "/Dining" or "/dining" are failing. For example, this redirect works fine-
Redirect 301 /healthfitness/teeth.cfm /healthcare/pretty-teeth
...as well as 100s of others.
But all of these are failing (many more than I'm showing)-
Redirect 301 /Dining/diningreviews/vawines.cfm /shopping/wines-2004
Redirect 301 /Dining/diningathome/carrotcake.cfm /home-garden/carrot-cake-2003
Redirect 301 /Dining/diningathome/oldvarolls.cfm /home-garden/virginia-rolls-2003
Redirect 301 /Dining/diningathome/pumpkincake.cfm /home-garden/pumpkin-cake-2003
The top of my .htaccess file looks like this-
RewriteEngine On
RewriteBase /
#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
#Everything below here are Redirect 301s
Dont redirect statements have to include the protocol in the destination?
You must include the domain name in the redirect.
Never mix mod_alias and mod_rewrite directives in the same file. If you use RewriteRule for any of your rules, you must use RewriteRule (not Redirect or RedirectMatch) for ALL of your rules.
List all redirects (using RewriteRule syntax) before any of the rewrites (using RewriteRule syntax) otherwise you will inadvertently expose rewritten pointers back out on the the web as new URLs.
The code you are currently using is very very inefficient. The .* patterns mean your .htaccess file will attempt hundreds of thousands of "back off and retry" trial matches for every URL request hitting the server.
In particular,
^(.\*/)? should be replaced by ^([^/]+/)\* in two places.
^.\*/ should be replaced by ^/([^/]+/)\*
!.\*wp-content/plugins.\* should be replaced by !wp-content/plugins
^([_0-9a-zA-Z-]+/)?(.\*\\.php)$ should be replaced by ^([_0-9A-Z-]+/)?([^.]+\\.php)$ with the [NC] flag also added.
The "add a trailing slash to /wp-admin" redirect should be the very first ruleset in the file and the target URL should include the protocol and domain name. That rule should be immediately followed with a non-www to www canonicalisation rule.
The new code may well run hundreds of times quicker.