Forum Moderators: phranque

Message Too Old, No Replies

.htaccess basic rewrite

         

glloeb

11:48 pm on Sep 1, 2008 (gmt 0)

10+ Year Member



Hello out there. I've been lurking around for a little while after beginning to attempt converting to extensionless urls. Anyway, I've had Apache mod_rewrite documentation open for 3 days or so and I can't figure out how to get this done. I've tried much of the code Jim posted, none of which seems to work in my case. I also attempted writing my own generalized code. The truth is, there are only a few pages I really care about making extensionless, so I'm willing to do it manually for those few. Unfortunately, I can't even get that to work. Here's what I'm working with:

RewriteEngine on
RewriteCond %{REQUEST_URI} /extensionlessurl [NC,OR]
RewriteCond %{REQUEST_URI} /exntensionless [NC]
RewriteRule ^(.+)$ http://example.com/$1.php [L]

I've also tried:
%{REQUEST_FILENAME} and all variations of ^/extensionlessurl$ or ^http://example\.com/extensionlessurl$

I know you guys get a lot of these, sorry for the clutter and thanks in advance for the help.
Gabe

jdMorgan

4:26 pm on Sep 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than start with a two-RewriteCond rule and an internal rewrite whose action cannot be directly seen, how about trying a simple first-pass test instead?

Try this, and let us know if it works:


RewriteEngine on
RewriteRule ^foo\.html$ http://www.google.com/ [R=301,L]

If you request "example.com/foo.html" from your server, where "example.com" is your domain, then you should land at google.com

This will confirm that *any* mod_rewrite code can run on your server. If it doesn't work, then it's a configuration problem, not a coding problem.

Jim

glloeb

4:58 pm on Sep 2, 2008 (gmt 0)

10+ Year Member



It's working. The same .htaccess is running a [www....] to http:// and a redirect from my site index to another page successfully.

jdMorgan

5:53 pm on Sep 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you did not explain anything about what you meant by "it didn't work," I'll hazard a guess that you got an "infinite" loop because your un-anchored patterns will the "/extensionlessurl.php" that results from your rewrite. Therefore the rule will loop.

Do this and see if it helps:


RewriteEngine on
RewriteCond %{REQUEST_URI} ^/full_extensionless/url-path from_Web_root_directory$ [OR]
RewriteCond %{REQUEST_URI} ^/another_full_extensionless/URL-path$
RewriteRule (.+) http://example.com/$1.php [L]

By specifying a full, anchored URL-path, there is no way it can match "xyz.php" and loop.

A URL-path from root means a path such as "/images/logo.gif" -- The path down from your root directory only, no domain or appended query strings.

Do not use [NC] to make the rule accept URLs with case errors. This potentially creates massive duplicate-content problems. Instead, if requested URL case errors are common, externally redirect the mis-cased URLs to the proper-cased URLs before applying this internal rewrite.

Concise tutorials for regular expressions are available through links in our forum charter, and in a post in our forum library.

Jim

glloeb

10:43 pm on Sep 2, 2008 (gmt 0)

10+ Year Member



It must have slipped my mind.. I was getting 500 Internal Errors, but I tried moving things around in the .htaccess file and got everything working: External/Internal redirects and removing tailing slashes from addresses that don't point to directories. Great!

I guess I just needed to cry wolf to get him away.

Thanks,
Gabe

jdMorgan

11:28 pm on Sep 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you get a 500-Server Error, look at your server error log file -- It will often tell you exactly what is wrong.

In this case, it's likely you had "Maximum internal redirection exceeded."

Jim

glloeb

11:58 pm on Sep 2, 2008 (gmt 0)

10+ Year Member



Oddly it wasn't showing any errors (forgetting to mention everything). I did run into the looping problem when trying to work my external redirects from an extra [OR], but I found it.