Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 Redirect for IP address with standard WordPress .htaccess file

For IP canonicalization, I'm told I need to redirect the IP address of the site to the domain name. I'm running a standard WordPress install that already comes with it's own .htaccess file. I modified it below by adding the "Redirect" line:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

Redirect 301 http://12.34.56.789 http://www.domainname.com

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

However, it's not working. Anyone know what is wrong?

Thanks!

like image 549
Yazmin Avatar asked Nov 14 '13 22:11

Yazmin


People also ask

What is WordPress 301 .htaccess redirect?

What is a 301 redirect in WordPress? A 301 redirect, or permanent redirect, is an HTTP status code for permanently moving a web page to a different URL. If someone types the old URL into their browser or clicks on a link that points to the old URL, your site will seamlessly take them to the new URL that you specify.

How do I redirect my site using a .htaccess file?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.


2 Answers

You generally don't want to mix Redirect (mod_alias) with RewriteRule (mod_rewrite) because they both get applied to the same URI and will clobber each others changes sometimes. Just stick with mod_rewrite because you have wordpress rules that already use it.

Replace the

Redirect 301 http://12.34.56.789 http://www.domainname.com

with:

RewriteCond %{HTTP_HOST} ^12\.34\.56\.789$
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]
like image 147
Jon Lin Avatar answered Nov 14 '22 21:11

Jon Lin


Jon Lin's answer worked for me but I had to use

RewriteCond %{REMOTE_ADDR} ^12\.34\.56\.789$
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]

instead of

RewriteCond %{HTTP_HOST} ^12\.34\.56\.789$
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]
like image 22
gravityboy Avatar answered Nov 14 '22 21:11

gravityboy