Forum Moderators: phranque

Message Too Old, No Replies

Redirect all files to a subfolder, htaccess

         

sendandreturn

8:48 am on Aug 19, 2011 (gmt 0)

10+ Year Member



Hi,

Currently I have this set up and running fine, searches on my domain TLD go to the NEW FOLDER


AddHandler phpini-cgi .php
Action phpini-cgi /cgi-bin/php5-custom-ini.cgi

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/www\.#*$!xx\.#*$!\/NEW-FOLDER" [R=301,L]


However, how can I redirect all other files that still exist and are searched on in the root (where the older site remains) to simply redirect to #*$!.#*$!/NEW-FOLDER

thanks..

sendandreturn

9:08 am on Aug 19, 2011 (gmt 0)

10+ Year Member



I have tried this, and the site still loads up, I'll check the error logs soon

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?tld.com$
RewriteCond %{REQUEST_URI} !^/NEW-FOLDER/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /NEW-FOLDER/$1
RewriteCond %{HTTP_HOST} ^(www.)?tld.com$
RewriteRule ^(/)?$ subdir/index.php [L]

lucy24

10:53 pm on Aug 20, 2011 (gmt 0)

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



RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/www\.#*$!xx\.#*$!\/NEW-FOLDER" [R=301,L]


You're coming to Apache from some other language, aren't you? You're escaping \ a bunch of things that don't need to be escaped-- including some that will be interpreted as literal backslashes.

%{HTTP_HOST} ^.*$


Would seem to mean "There may or may not be a host" (making the whole line superfluous). What is it intended to mean?

RewriteRule ^/?$ "http\:\/\/www\.#*$!xx\.#*$!\/NEW-FOLDER" [R=301,L]


Request consists of a single slash, or nothing-- in other words, request is for domainname alone. (This would not work on my server, but may work for you.)

rewrite_rule doesn't need quotation marks.

In the target, nothing is escaped. Even in patterns, colons and slashes do not need escaping. (I smell Javascript ;)) What you do need to escape in all patterns is the literal period . so you don't end up matching "tldxcom" and the like.

So in your first post you're rewriting to

http://www.example.com/NEW-FOLDER

RewriteRule ^(.*)$ /NEW-FOLDER/$1


This should work. Combined with the conditions, it means "take any and all incoming requests except the ones that already say /NEW-FOLDER/ and shunt them over to NEW-FOLDER."

Note that you don't actually need the ^ and $ anchors. By default, Regular Expressions will capture everything in sight.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


Do the files in their original locations (the ones without NEW-FOLDER) still exist? If so, you can't use this pair of conditions, because they mean "apply the rule only if the requested file or directory doesn't exist". In fact you may need the other pair, -f and -d without !, meaning "If there is a request for a real file or directory". Just keep careful track of /NEW-FOLDER/ to avoid infinite loops.

sendandreturn

9:12 am on Aug 22, 2011 (gmt 0)

10+ Year Member



Hi, thanks for getting back to me.

I added the line you mentioned, removing my original, and this happened when I browsed for a category off the home page menu...

[TLD...]
I ended up with a double slash and a 400 error...

so I have put my original lines back in and when I type in TLD.net it redirects to TLD.net/NEW-FOLDER which is what I am after...however if it I type in TLD.net/oldfile, it does not go to TLD.net/NEW-FOLDER/oldfile still...so I am kinda halfway there...

g1smd

9:24 am on Aug 22, 2011 (gmt 0)

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



I can't read the code with all the #*$! in it.

Always include the domain name in the target of a redirect. Use example.com here in the forum to supress the auto-linking function.

Escape all literal periods in patterns.

Add a blank line after every RewriteRule, and then # comment in plain English what each block of code is supposed to do.

sendandreturn

10:30 am on Aug 22, 2011 (gmt 0)

10+ Year Member



Sorry, I didn't know it would do that, plus sorry for all of the / and the \'s in what I have currently, it works for redirecting example.com to example.com/NEWFOLDER at the mo..

This is what I have currently

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


I have the old site in the root still, as I have moved it to NEW FOLDER, and from the error logs i can see plenty of accesses to files and directories that exist in the root still, and I would like to append NEW-FOLDER to them after example.com, so they don't 404

sendandreturn

10:36 am on Aug 22, 2011 (gmt 0)

10+ Year Member



I must add that I have two htaccess's in place, one in the root and one in the new site...all of the above is in the root htaccess, the sub dir access has rewritebase /NEWFOLDER in it, does that make a difference?

sendandreturn

11:07 am on Aug 22, 2011 (gmt 0)

10+ Year Member



I added the below, as per the advice in the first response I had, and it just loaded up the older site in the root...


RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /NEW FOLDER/$1

g1smd

12:57 pm on Aug 22, 2011 (gmt 0)

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



Every RewriteRule needs the [L] flag.

You haven't fixed most of the things that have already been pointed out. They are all important. Even if you think the code is "working" the corrections are there to fix other problems that you are not yet even aware of.

Fix every last thing mentioned so far then post the entire fixed version of the code.

sendandreturn

1:20 pm on Aug 22, 2011 (gmt 0)

10+ Year Member



I have, look at my post above yours, I have tried it, and again admittedly with the [L} included, and I get:

a) example.com/feed does not redirect to example.com/NEWFOLDER/feed, i get a 500

b) The site loads up as example.com, loading up the correct new site, however when I browse any category I get a 500 error due to a double forward slash

[tld...]

I wish i could fix everything mentioned so far, but I can't, hence I am posting here...I am trying and providing as much background info as I can

sendandreturn

1:30 pm on Aug 22, 2011 (gmt 0)

10+ Year Member



When I implemented the above I got in my error logs..
mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.

g1smd

2:52 pm on Aug 22, 2011 (gmt 0)

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



You have in infinite loop because the new pointer matches the pattern and the rule runs again. You need to exclude already rewritten requests by using a preceding RewriteCond.

Use example.com here in the forum to suppress the auto-linking function.

Fix every last thing mentioned so far then post the entire fixed version of the code.

sendandreturn

3:11 pm on Aug 22, 2011 (gmt 0)

10+ Year Member



I have no idea what you mean, so.......I'm off to another forum...