Forum Moderators: phranque

Message Too Old, No Replies

can't redirect my blog htaccess

         

adsalamon

4:01 pm on Mar 16, 2015 (gmt 0)

10+ Year Member



I have a website with 3 domains. i have set it up so that example1.com and exmaple 2.com point to example.com

I also have a blog page withing this site at example.com/blog

The prob is that I cannot redirect example1.com/blog to example.com/blog it justs stays at the address you types in

Hope this is making sense


ewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]


RewriteEngine on

RewriteCond %{HTTP_HOST} ^91\.238\.160\.4
RewriteRule (.*) http://www.example\.co\.uk/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^examlpe1\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.co\.uk$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example2\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example2\.co\.uk$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.co\.uk$
RewriteRule ^/?$ "http\:\/\/www\.example\.co\.uk$1" [R=301,L]



hope someone can help with this

not2easy

6:13 pm on Mar 16, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Are these WordPress blogs? The CMS does make a difference so it helps to know that.

lucy24

6:19 pm on Mar 16, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<tangent>
It is never necessary to escape slashes / or colons : in mod_rewrite. It is never necessary to escape anything in a RewriteRule target.
</tangent>

You've got way too many anchors. If your aim is to redirect all requests, why not simply
RewriteCond %{HTTP_HOST} (wrongexample1|wrongexample2)
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]

or better yet, simply include it in the existing domain-name-canonicalization redirect, positioned as your last [R] redirect in htaccess:
RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]

Caveat: This is the simplest approach, but you can only use it if the htaccess is only intended for a single site.

adsalamon

11:13 pm on Mar 16, 2015 (gmt 0)

10+ Year Member



no sorry not wordpress.plain php and mysql.

adsalamon

11:17 pm on Mar 16, 2015 (gmt 0)

10+ Year Member



tried the above code. redirects the sites but not the blog section. the blog main page is index.php inside a folder called blog so www.example.co.uk/blog or www.example.co.uk/blog/index.php

the redirect works for other folders

lucy24

1:38 am on Mar 17, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Where are the blog files physically located? htaccess applies to physical filepaths, not to URLs. If the directory containing /blog/ is not inside the directory that contains the htaccess file, requests will never see your RewriteRules.

Oh yes and...
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]

This rule-- and any similar [L] rules-- must go after the external redirects. Once a request has met this rule, nothing else can happen.

adsalamon

9:11 am on Mar 17, 2015 (gmt 0)

10+ Year Member



getting closer. now redirects the main blog page but clicking rea dmore to view blog full message not loading. think that has to do with the rewrite pretty url thing..

adsalamon

9:27 am on Mar 17, 2015 (gmt 0)

10+ Year Member



moved htaccess redirect to blog folder and seems to be ok now :D

lucy24

5:38 pm on Mar 17, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, wait. You've got RewriteRules in two places along the same path? Don't ever do that...

adsalamon

5:59 pm on Mar 17, 2015 (gmt 0)

10+ Year Member



hmm don't know what to do then

adsalamon

1:07 am on Mar 18, 2015 (gmt 0)

10+ Year Member



i'm overwhelmed by all this!

so far i have a htaccess in my my public_html
This has -

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]

I also have a htaccess in my blog folder which also has

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
but also
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]

if i remove anything from either than nothing works

lucy24

2:01 am on Mar 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if i remove anything from either than nothing works

Be prepared for a lot of duplication, then. "RewriteOptions inherit" rarely works as intended. So any rule that exists in your root htaccess must be duplicated in your blog htaccess (like your HTTP_HOST rule) and, conversely, any rule that's intended to work on both the blog and on other areas must be duplicated from the blog htaccess to the root htaccess. If you can't combine all your RewriteRules into a single htaccess-- which should absolutely be feasible, but is a different question-- then you have to put everything in both places. (Excluding, of course, requests for inner directories other than /blog/ because those will only ever see one set of RewriteRules.)

But if you've already got a domain-name-canonicalization redirect in both places, that should take care of everything. Is there a remaining problem?

adsalamon

1:05 pm on Mar 18, 2015 (gmt 0)

10+ Year Member



*confused* :D

lucy24

6:51 pm on Mar 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, let's backtrack. Right now you say you've got two htaccess files: one in the root, and one in the /blog/ directory which is inside the root. Each of those two contains the element

RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]


This, by itself, should be sufficient to redirect requests for other domains whose DNS is pointed to the same place.

So what's the remaining problem? Seems like everything should already be working as intended.

Edit:
Oops, except that the one inside the /blog/ directory can't be identical. It has to say

RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule (.*) http://www.example.co.uk/blog/$1 [R=301,L]


because the capture (the pattern) is based on the location of the rule.

adsalamon

2:07 am on Mar 19, 2015 (gmt 0)

10+ Year Member



thanks. everything is working fine now!