homepage Welcome to WebmasterWorld Guest from 54.226.173.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod_rewrite help please
moved files and need to rewrite old url
astrogirl2




msg:1498072
 6:47 am on Nov 28, 2002 (gmt 0)

Here's my old setup:

/catalog/default.php

new setup:

/index.php (I renamed default.php to index.php)

Problem is that I have search engine listings that look like this:
www.mysite.com/catalog/default.php?cPath=42
that now return a page not found error.

I tried rewriting the URL with:
rewriteEngine on
rewriteBase /catalog
rewriteRule ^/catalog/default\.*$ index.php [L]

I also tried with rewriteBase /

I tried the .htaccess file in both /catalog and in /. Either worked. They still returned page not found.

mod_rewrite is an installed module at my host.

Any help on how to get this correct would be greatly appreciated.

Thanks, Astrogirl

 

jdMorgan




msg:1498073
 8:17 am on Nov 28, 2002 (gmt 0)

astrogirl2,

Rewrite /catalog/default.php to /index.php using .htaccess in top-level site directory:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^catalog/default\.php$ /index.php [R=301,L]

The problems were with the leading slashes and with the "default\.*$", which means "default. followed by an asterisk." The backslash escapes the period, meaning "include the period as a literal character to be matched." You could have successfully written that as:

default\..*$
where \. means "literal period" and .* means "anything", but just leaving the ".*" and the end anchor "$" off does the same thing. Check out this short regular expressions tutorial [etext.lib.virginia.edu].

Also, you shouldn't normally need RewriteBase unless your server is set up funky.

HTH,
Jim

astrogirl2




msg:1498074
 1:44 pm on Nov 28, 2002 (gmt 0)

Thanks JD and Happy Thanksgiving down there in Texas. It's a bit chilly up here in Mass. Snow and all :-(

I tried your suggestion and the click through still returns a page not found. Something about the "?cPath=42" at the end of the URL, no?

astrogirl

jdMorgan




msg:1498075
 3:42 pm on Nov 28, 2002 (gmt 0)

astrogirl2,

Please be as specific as possible... Do the existing querystrings also need to be preserved? Or modified? What was wrong with the result URL when you tried the rewrite above?

Jim

astrogirl2




msg:1498076
 3:56 pm on Nov 28, 2002 (gmt 0)

JD,

Starting over... Here's what I'm looking for:

Request:
www.mysite.com/catalog/default.php?cPath=42
www.mysite.com/catalog/default.php?cPath=21
www.mysite.com/catalog/default.php?cPath=22
etc.

Result:
www.mysite.com/index.php

OR, if doable, for the corresponding Requests above, it would be nice to get the Results:

www.mysite.com/index.php?cPath=42
www.mysite.com/index.php?cPath=21
www.mysite.com/index.php?cPath=22
etc.

Thx, ag

jdMorgan




msg:1498077
 4:17 pm on Nov 28, 2002 (gmt 0)

ag,


Request:
www.mysite.com/catalog/default.php?cPath=42
www.mysite.com/catalog/default.php?cPath=21
www.mysite.com/catalog/default.php?cPath=22
etc.

Result:

www.mysite.com/index.php?cPath=42
www.mysite.com/index.php?cPath=21
www.mysite.com/index.php?cPath=22

This is exactly what the rewrite I posted should do. At least, it works for me! The query string has been moved to an environment variable by the time the server gets to this phase of request processing, so it should not be affected at all by the rewrite...

What went wrong? What was the exact URL that was requested after the rewrite and caused the 404?

Jim

P.S. Cold here, too! No snow - a little ice this A.M.

jdMorgan




msg:1498078
 4:40 pm on Nov 28, 2002 (gmt 0)

ag,

One more thing... You can change the RewriteRule for testing purposes if you want to avoid messing up visitors to your site while you debug this problem...

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^catalog/de[b]e[/b]fault\.php$ /index.php [R=301,L]

Then, just request that nonexistent "deefault.php?cPath=42" page manually from your browser, and see what the output URL is when/if you get a 404.

HTH,
Jim

astrogirl2




msg:1498079
 5:28 pm on Nov 28, 2002 (gmt 0)

Here's the URL that was requested after the rewrite that caused the 404:

[mysite.com...]

which is exactly how the URL appears in the SE listing.

Thank you for your persistence.

ag

BTW: for what it's worth; here's the contents of my /.htaccess:

<IfModule mod_setenvif.c>
<IfDefine SSL>
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
</IfDefine>
</IfModule>

Options -Indexes

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^catalog/default\.php$ /index.php [R=301,L]

ErrorDocument 404 [mysite.com...]

jdMorgan




msg:1498080
 5:45 pm on Nov 28, 2002 (gmt 0)

ag,

If the URL output from mod_rewrite is the same as the requested URL, then it's likely that mod_rewrite is not enabled, even though it is loaded on your server.

You could try a simple rewrite as a test - something like:

RewriteRule \.junk$ http://www.mysite.com/index.php [R=301,L]

Then request a page called "abc.junk" and you should be redirected to your home page. The lack of a start anchor on the pattern and the full path specification on the target URL should eliminate almost all dependencies on site structure.

It does sound like mod_rewrite is not enabled - Contact your host if the above does not work. :(

Jim

P.S. You can combine the Options: Options +FollowSymlinks -Indexes

astrogirl2




msg:1498081
 2:59 am on Nov 29, 2002 (gmt 0)

I'm baaack.

I tried the "junk" test you recommended and I was successfully redirected to the home page upon typing .../abc.junk in the browser. So, that appeared to work correctly.

Anything else to try?

ag

jdMorgan




msg:1498082
 4:01 am on Nov 29, 2002 (gmt 0)

ag,

Weird!

Note important correction to your ErrorDocument directive - It must be a local path as shown below, not a URL! (I doubt this has anything to do with the rewrite problem, but it can seriously mess up your SE listings!)

OK, so if you put the

Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteRule ^catalog/default\.php$ /index.php [R=301,L]
ErrorDocument 404 /custom.php

code back in, and request the page:
[mysite.com...]
will you see a request for
[mysite.com...]
in your access log, and also in your error log?

Please confirm.

------

If you end up waiting for a reply, you might try these next two test rules as well... The above rule should work in .htaccess, and I can't figure out why it doesn't. The first rule below should work in httpd.conf. The 2nd rule below is "sloppy" and should work in either, but I don't recommend it for "permanent" use.

RewriteRule ^/catalog/default\.php$ /index.php [R=301,L]
RewriteRule catalog/default\.php$ /index.php [R=301,L]

------

When you log into your site using ftp, is the directory name "public" or "username" or anything like that? If so, you may need the same string in a RewriteBase directive.

Jim

astrogirl2




msg:1498083
 5:24 am on Nov 29, 2002 (gmt 0)

Jim,

Here's the entry from the access log:
..."GET /catalog/default.php?cPath=42 HTTP/1.1" "-" 404 14203

Here's the entry from the error log:
File does not exist: /usr/local/plesk/apache/vhosts/mysite.com/httpdocs/catalog/default.php (there is no?cPath=42)

I don't have write access to httpd.conf with my host, so I couldn't try that RewriteRule. The second RewriteRule you suggested didn't work.

When I log in with ftp, I land at / and there are a bunch of directories - bin, cert, cgi-bin, conf, httpdocs, httpdocs, logs, pd, and webusers

ag

jdMorgan




msg:1498084
 5:37 am on Nov 29, 2002 (gmt 0)

Ag,

Aughhh! httpdocs!

Try this:

RewriteBase /httpdocs
Options -Indexes +FollowSymlinks
RewriteEngine on
[b]RewriteBase /httpdocs[/b]
RewriteRule ^catalog/default\.php$ /index.php [R=301,L]
ErrorDocument 404 /custom.php

Gotta retire - ate too much today (of course). I hope this helps, and if not - manyana!

Jim

astrogirl2




msg:1498085
 5:54 am on Nov 29, 2002 (gmt 0)

I tried this rule:
RewriteRule ^default\.php$ /index.php [R=301,L]

put www.mysite.com/default.php and it correctly rewrote to www.mysite.com/index.php

It doesn't like catalog/ in front of default

ag

astrogirl2




msg:1498086
 6:04 am on Nov 29, 2002 (gmt 0)

Thank you sooo much for your time and help, though your last suggestion didn't work either :-(

I'll keep reading...

ag

jdMorgan




msg:1498087
 6:09 am on Nov 29, 2002 (gmt 0)

ag,

Which directory in this path is the .htaccess file we've been discussing in?

mysite.com/.htaccess ,
httpdocs/.htaccess , or
catalog/.htaccess?

From your last post, it sounds like it's "catalog/.htaccess", whereas I assumed it would be "httpdocs/.htaccess" in my last post, and was assuming previously that it was "mysite.com/.htaccess"

Eyes held up w/toothpicks... :)

Jim

astrogirl2




msg:1498088
 6:24 am on Nov 29, 2002 (gmt 0)

I have it in
httpdocs/.htaccess AND
catalog/.htaccess

I get permission denied at /

jdMorgan




msg:1498089
 6:49 am on Nov 29, 2002 (gmt 0)

OK,

Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteRule ^catalog/default\.php$ /index.php [R=301,L]
ErrorDocument 404 /custom.php

should reside and should work in /httpdocs/.htaccess, but it might need the
RewriteBase /httpdocs
directive to be included, too.

I would not expect the above to work in /httpdocs/catalog/.htaccess - with or without RewriteBase.

I'll check in again Friday A.M. :)

Jim

astrogirl2




msg:1498090
 1:59 am on Nov 30, 2002 (gmt 0)

Hi Jim,

I tried your last suggestion in httpdocs/.htaccess with/without RewriteBase /httpdocs. No luck. I don't get it, this worked fine:
RewriteRule ^default\.php$ /index.php [R=301,L]

Also, my host did confirm that the module is enabled.

ag

jdMorgan




msg:1498091
 2:13 am on Nov 30, 2002 (gmt 0)

ag,

Curiouser and curiouser!

So you have

RewriteRule ^default\.php$ /index.php [R=301,L]

in your .htaccess file in your httpdocs directory, and it's working?

If so, maybe your host set up an Alias somewhere - that's all I can think of to explain it!

If you figure this out - tomorrow or a year from now - please post or sticky me... I'd love to know where the "disconnect" is.

Jim

astrogirl2




msg:1498092
 4:11 am on Nov 30, 2002 (gmt 0)

Bad wording on my part in that last message. That rewrite rule worked as a test, but it doesn't do what I need. Sorry. Thanks for all your help. I don't really expect you to continue this with me. If I ever do get it, I'll let you know.

ag

jdMorgan




msg:1498093
 4:36 am on Nov 30, 2002 (gmt 0)

ag,

in your .htaccess file in your httpdocs directory, and it's working?

That's the part that I'm dyin' to know...

Jim

astrogirl2




msg:1498094
 5:55 am on Nov 30, 2002 (gmt 0)

At it again...

Yeah, I have this in httpdocs/.htaccess:

Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteRule ^default\.php$ /index.php [R=301,L]
ErrorDocument 404 /custom.php

put www.mysite.com/default.php in my browser and it rewrites to www.mysite.com/index.php

When I make the rule ^catalog/default\.php$ ... and try www.mysite.com/catalog/default.php in my browser I get the page not found and land on custom.php.

ag

jdMorgan




msg:1498095
 1:20 am on Dec 1, 2002 (gmt 0)

ag,

Well, that should work, but something is making it not work. I'd give 10 bucks to be able to see over your shoulder - this is weird! And frustrating! It sounds like something, somewhere is aliasing the /catalog/ path.

If you figure that part out and need help on some other detail, please post.

Jim

astrogirl2




msg:1498096
 10:10 pm on Dec 1, 2002 (gmt 0)

jd,

Here's what ended up working:

Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteRule ^catalog/default\.php$ /index.php [R=301,L]
ErrorDocument 404 /custom.php

You recommended this and I tried it days ago and it didn't work. I then started trying a bunch of different iterations including removing the "/" before index.php. I also tried "test.php" on the RHS w/out the "/" and noticed a wierd URL string usr/plesk/vhosts/blah blah blah/test.php. I then added the "/" back in and got the same URL. I then changed test.php back to index.php and IT WORKED!

I absolutely don't get this. The positive - I've read a ton of material in the last couple of days on mod_rewrite because I was determined to get this to work. Maybe taking so long was a blessing in disguise.

Thanks again,

ag

jdMorgan




msg:1498097
 11:21 pm on Dec 1, 2002 (gmt 0)

ag,

I'm really glad to hear that it worked - I thought I was losing it, or that I'd given you bad info!

Every time I post a rewrite here, I dread the possibility of looking at it a few days later and seeing a typo - 'Cause it has to be perfect or it won't work! Anyway, now you've got another very versatile tool in your toolbox, and I'm glad if I was of some help.

Take care,
Jim

hetzeld




msg:1498098
 6:14 am on Dec 13, 2002 (gmt 0)

Hi astrogirl,

I guess that this could work better:

RewriteRule ^catalog/default\.php(.*)$ /index.php$1 [R=301,L]

or
RewriteRule ^catalog/default\.php /index.php [R=301,L]

This will pass your query string untouched.

Dan

jdMorgan




msg:1498099
 5:41 pm on Dec 13, 2002 (gmt 0)

Dan,

By the time Apache reaches this API phase - processing .htaccess and inherited directory-level directives, the query string part has been removed and placed in a system variable, accessible in mod_rewrite via %{QUERY_STRING}.

Therefore, if you wish to test and use the query string part in .htaccess, you have to refer to it as %{QUERY_STRING} and it cannot be directly "seen" by the pattern-matching/back-reference definition process in a RewriteRule. It can be used for pattern matching and to define a back-reference in a RewriteCond, however, and then back-referenced in a RewriteRule using "%1" instead of "$1".

So, for example, you could do:

RewriteCond %{QUERY_STRING} ^ID=newuser(.*)$
RewriteRule ^catalog/default\.php$ /index.php?newusernumber=%1 [R=301,L]

To backreference it, or

RewriteRule ^catalog/default\.php$ /index.php?new%{QUERY_STRING} [R=301,L]

to refer to it by name.

But the query string is no longer directly available for RewriteRule pattern-matching or back-reference definition at this point in the request processing - The use of RewriteCond or named reference in the target URL string is required to process query strings in .htaccess.

Jim

<edit>ag2, the above example is [i]only[]/i an example; The rewrite shown doesn't apply to what you wanted to do.</edit>

astrogirl2




msg:1498100
 3:14 pm on Dec 15, 2002 (gmt 0)

Hi Dan,

Thanks for the other alternatives. I ended up getting it to work with RewriteRule ^catalog/default\.php$ /index.php [R=301,L] . I can only hope that this becomes a moot point once the Dec deep crawl takes effect. My site got hit 700+ times by googlebot vs. 3 times all other crawls.

ag

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved