PDA

View Full Version : Mod Rewrite and .htaccess


bluemicrobyte
03-05-2006, 7:46 PM
So I've figured out how to use:

RewriteRule ^(.*).php ../../forums/admin/$1.php

Lets me "mirror" php files in another directory. I've also been able to use that for .gif, .jpg, .swf, and any other file extention I please.

But that code will try to "get" the file from the alternate directory all the time - even if the file exists in the first directory.

How can I use mod-rewrite so that IF the file doesn't exist, THEN it looks for it in the alternate directory?

And can I even get more complex and say IF the file doesn't exist, check for it in directory 1, and if that doesn't exist, check for it directory 2?

Original problem solved. Scroll down 2 posts for new problem.

GrassDragon
03-05-2006, 8:26 PM
I don't think you can do that with mod rewrite. That simply tells the browser to go to www.siteone.com/directory when someone types in www.sitetwo.com/directory.

Seal
03-06-2006, 12:23 AM
rewrite does it server-side so the user wouldnt know, GD.

try having special rules for each modified .php file, before the last (catch-all) rule

bluemicrobyte
03-26-2006, 3:29 AM
Original problem solved. New problem:

RewriteRule ^(stuff1|stuff2).html $1.php

lets stuff1.php (physical file) appear as stuff1.html but you can still type stuff1.php into your browser and have it show up. Can I make it so that when you type stuff1.php into your browser you get a permanantly moved error or something else so that you can only access the file via stuff1.html? (even though the physical file is stuff1.php)

GrassDragon
03-26-2006, 3:12 PM
You could forward any requests for *.php files to an error page. I think you can do that with .htaccess.

bluemicrobyte
03-26-2006, 5:43 PM
How do I do that without messing up the first rule? I tried something similar to that and it didn't work.

GrassDragon
03-26-2006, 6:50 PM
I really don't have that much experience with it, sorry. Maybe you could modify one of the commands that's intended to forwards a subdomain to a directory so that it will forward your *.php's to your error page?

Jeff
04-05-2006, 5:06 PM
I don't know much about htaccess but you should be able to do the redirect part in PHP.


// If the last 3 characters in the URL are ".php"
if(substr($_SERVER['REQUEST_URI'], -4) == ".php") {
// Redirect to the .html URL
header("Location: ".substr($_SERVER['REQUEST_URI'], 0, -4).".html");
}


Hopefully that works in conjunction with your htaccess, I haven't tried it. Good luck.

bluemicrobyte
04-14-2006, 2:16 PM
Thanks, Jeff, that seems to work - but will that also redirect search engines to the html pages? The main reason I'm doing all this is for search engine optimization since they like html pages better.

Jeff
04-17-2006, 4:09 AM
Thanks, Jeff, that seems to work - but will that also redirect search engines to the html pages? The main reason I'm doing all this is for search engine optimization since they like html pages better.

Search engines not liking .php pages may have been a problem that once existed, but I really don't think it's much of an issue anymore.

If anything, a search engine bot would probably rather have a .php URL than have to go through strange redirects. If you use the code I posted, the search engine bot would know that a redirect was occuring, and that is something that should be avoided.

I think you're better off leaving everything as .php.

That said, if you want to keep your .html URLs, another option is to have all .html files be processed as if they were .php. This can be done with the following line in your .htaccess:

AddType application/x-httpd-php .php .htm .html

I think that should accomplish what you're looking to do.

bluemicrobyte
04-17-2006, 10:15 PM
For about two months I had everything as .php but the search engines weren't indexing the pages very well - google seemed to randomly add or remove my pages, and then eventually it just dropped all of them. Since I implemented the html mod rewrite my pages have been indexed by google much more quickly, so I think for now I'll stick with that and see how it works.

bluemicrobyte
05-22-2006, 10:16 PM
Another .htaccess problem ---

The URL http://www.microbyteproductions.com/murloc will return a 404 not found error.

The UTL http://www.microbyteproductions.com/murloc/ works perfectly fine. When I remove the .htacess file from that directory, the first URL works fine as well.

This is what's in the .htacess file:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^(.*).html $1.php


What can I add or change to get both of the above URLs to work?