Forum Moderators: phranque

Message Too Old, No Replies

Moving up one directory with mod rewrite

         

concepts99

8:33 pm on Aug 25, 2007 (gmt 0)

10+ Year Member



Hello,

I want to move everything up one directory

Original

[webname...]

to

[webname...]

How can I do this by just TAKING out SUBDIRECTORY2. I also want to make sure it is a 301 to the search engines, any help?

jdMorgan

8:40 pm on Aug 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post the best-effort code you've written and tested as a basis for discussion.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Thanks,
Jim

concepts99

8:52 pm on Aug 25, 2007 (gmt 0)

10+ Year Member




RewriteEngine on

RewriteRule ^/subdirectory2/(.*)$ [domain.com.com...] [R=301,L]

I am trying something like this, everything after the subdirectory 2 name and then redirecting to the new url WITHOUT the subdirectory2 in it, but it doesnt seem to be working.

I am editing the .htaccess in the subdirectory1 directory since subdirectory 2 (which was under subdirectory 1) no longer exists.

I also have a ErrorDocument 404 [domain.com...]

When I try to

[domain.com...]

it sends me to the errordocument 404 url

jdMorgan

9:09 pm on Aug 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



URL-paths as seen by RewriteRule in .htaccess do not include the leading slash, so in /subdirectory1/.htaccess you'll need:

RewriteRule [b]^sub[/b]directory2/(.*)$ http://www.example.com.com/subdirectory1/$1 [R=301,L]

The syntax of your ErrorDocument is incorrect, and will result in a 302-Found server response, rather than the desired 404-Not Found. Correcting what you've got, it should read:

ErrorDocument 404 /subdirectory1/

However, I must note that from a search-engine-ranking and site usability standpoint, it is a bad idea to use your index page as an ErrorDocument; You confuse your visitors, and search engine robots may see duplicate content (more than one URL produces the same content). I recommend that you use a specific error document which explains that the requested resource is missing and provides text links to a relevant category, your site map, or your home page, as applicable.

Note that if you intentionally remove resources from your site, the URLs for those resources should return a 410-Gone response, since 404-Not Found means "The requested resource is missing for an unknown reason" and could be due to either a client error, a Webmaster error, or a server error. 410-Gone says "This resource was intentionally removed and will not return -- At least not any time soon." In order to return a 410-Gone status, you must add code that detects the URLs of removed resources, and produces the 410 response accordingly.

Jim

concepts99

9:27 pm on Aug 25, 2007 (gmt 0)

10+ Year Member



Thanks for the tip on the leading slash, that got me.

Also the info on the syntax, I have adjusted it to another page and fixed it as well. This is the second time this forum has helped me and I am glad I donated/subscribed and would recommend it to any readers or lurkers present.

g1smd

4:16 pm on Aug 26, 2007 (gmt 0)

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



If the files that run the site are in a folder like:

/subdirectory1/subdirectory2/

then you could use a rewrite (instead of a redirect) to allow URLs like www.example.com/subdirectory1/ to access that content.

The links on the pages must also be changed to call those www.example.com/subdirectory1/ URLs too.

concepts99

4:47 pm on Aug 27, 2007 (gmt 0)

10+ Year Member



I have a new problem now. I am getting SQL errors

Description : Unknown column '26js' in 'where clause'
Request URI: /x/product.php?productid=1402%2526js=n

%2526 should be the & sign, so it should be /x/product.php?productid=1402&js=n

Another example of this is
Description : Unknown column '26cat' in 'where clause'
Request URI: /x/product.php?productid=1062%2526cat=110%2526page=1
Backtrace:

It should be (there are 2 instances of %2526 in the example above)
/x/product.php?productid=1062&cat=110&page=1

I assume the previous rewrite should retain the & sign as an & sign and not convert it to the equivalent of %2526

This is really bogging down my sql processes, can I make another rewrite to capture the %2526 and convert it to an & sign

Does it get amended to the END of the rewrite of

RewriteRule ^subdirectory2/(.*)$ http://www.example.com.com/subdirectory1/$1 [R=301,L]

or does it get added UNDERNEATH as a separate rewrite rule?

Any help would be appreciated as I am stuck and need to stop these sql errors.

jdMorgan

10:34 pm on Aug 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hoo boy! Another one of those recursively-encoded URL problems.

The first thing to try is to add the "NE" flag to the RewruteRule's flags, making it [NE,R=301,L]. This prevents the escaping/encoding of various characters, and may help with this problem ("%25" is a hex-encoded "%" character, and %2525 is a double encoded "%" character).

Otherwise, you may have to detect and 'manually' unencode strings like %252526 to %2526, then %26 and then to "&". This is ugly and inefficient unless you have access to the system "unencode" function via RewriteMap in httpd.conf, and should be avoided if possible.

Jim