Forum Moderators: phranque

Message Too Old, No Replies

redirect conflicts with RewriteCond

         

vampke

9:17 am on Dec 17, 2013 (gmt 0)

10+ Year Member



Hello,

I need to redirect all the pages of 2 sites to a new site's subfolder.
The redirect seems to be conflicting with the rewriteconditions on the site that is being redirect to.
The receiving site uses a cms that makes nice looking URL's with this code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


If I redirect my old site's pages using
redirect 301 / http://www.mywebsite.tld/dir/

the pages are redirected to
http://www.mywebsite.tld/dir/index.html?q=dir/index.html
which does not exist

I have tried doing to redirect using
RewriteEngine on
RewriteRule ^(.*)$ http://www.mywebsite.tld/dir/index.php?q=$1 [L]
which redirects to
http://www.mywebsite.tld/dir/index.php?q=pagename.html
which works, but is not a pretty url (possibly i need to add some lines in htacces of the redirect site to fix).

The above htaccess does not work however for another site I need to redirect to the same folder because the site is located in the root:
www.mywebsite.othertld

RewriteEngine on
RewriteRule ^(.*)$ http://www.mywebsite.tld/dir/index.php?q=$1 [L]
redirects to
http://www.mywebsite.tld/dirpagename.html
without the / after
dir


Any help would be greatly appreciated and rewarded with beer

g1smd

1:48 pm on Dec 17, 2013 (gmt 0)

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



The new rules will need to be placed BEFORE your existing rules.

The RegEx pattern for the new rule(s) will need to match whatever the old URL request is. The rule target, with protocol and hostname, is the URL that the user should see in their browser address bar after the redirect.

Use RewriteRule for all of your rules. Do not use Redirect or RedirectMatch.

vampke

2:51 pm on Dec 17, 2013 (gmt 0)

10+ Year Member



thanks for your reply g1, I realize my post may not have been entirely clear.
The regex conditions are on the site that is redirected to. Not on the site I want to redirect.
The site I want to redirect only had the redirect 301 rule in its htaccess

g1smd

3:11 pm on Dec 17, 2013 (gmt 0)

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



Are the various sites on the same server or on different servers?

vampke

3:38 pm on Dec 17, 2013 (gmt 0)

10+ Year Member



different servers

lucy24

10:09 pm on Dec 17, 2013 (gmt 0)

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



This line
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

is standard in boilerplate CMS htaccess but it's way overkill. Does the CMS really handle requests for images or stylesheets? If not, fine-tune the "pattern" part of the rule so it only kicks in when the request is for a page (extension html or php or possibly nothing) or directory.

That being said: Your boilerplate has the standard RewriteCond involving !-f and !-d meaning "do not invoke this rule if the request was for a file or directory that physically exists". So it's a bit of a mystery why this happens:

http://www.example.com/dir/
>>
http://www.example.com/dir/index.html?q=dir/index.html

In fact I don't see how it's physically possible unless you've set a RewriteBase in the new htaccess to /dir/

That's assuming your /dir/ --the ones that other sites are getting redirected to-- are real, physical directories. If they don't physically exist and are merely part of the URLpath to be handled elsewhere then you'd need an additional Condition

RewriteCond %{REQUEST_URI} !^/(dir1|dir2)

where dir1 and dir2 are your redirect targets.

But that's only if your dir1 and dir2 don't physically exist. In which case: where's the code handling the requests?

Does your new site have more than one htaccess file? In shared hosting this is usually possible and often necessary-- BUT don't have more than one set of RewriteRules along the same path.

vampke

4:30 pm on Dec 18, 2013 (gmt 0)

10+ Year Member



apple's new product: the iBlush...
Apparently browsers cache redirects! I did not know this...
The 301 redirect from www.mywebsite.othertld to
http://www.mynewwebsite.tld/dir 
just work!
The pages are perfectly redirected to the corresponding pages on the new site.
sorry for the trouble and thanks for your efforts to get me sort this out.
I hope this thread will help someone else... use private mode to test!

g1smd

6:37 pm on Dec 18, 2013 (gmt 0)

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



As coded, your redirects return 302, not 301 status.