Forum Moderators: phranque
/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
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]
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
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
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.
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
[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...]
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]
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
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
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
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
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
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
Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteRule ^catalog/default\.php$ /index.php [R=301,L]
ErrorDocument 404 /custom.php
RewriteBase /httpdocs 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
Curiouser and curiouser!
So you have
RewriteRule ^default\.php$ /index.php [R=301,L]
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
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
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
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
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
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]
RewriteRule ^catalog/default\.php$ /index.php?new%{QUERY_STRING} [R=301,L]
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>