Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule help request

Had it working at some point...

         

Chrispcritters

11:30 pm on Sep 29, 2011 (gmt 0)

10+ Year Member



I had the rules working at some point but over several years I've apparently broken one or more of them.

Here are the things I'm trying to achieve:
  • www.example.com to example.com
  • Force all to lowercase
  • drop php extension
  • default page is index.php

So...

www.ExAmPlE.com/DiR/PaGe.php -> example.com/dir/page
www.EXaMpLe.com/index.php -> example.com
www.ExAmple.com/DIR/index.php -> example.com/dir/

I can post my existing code replacing the domain but there are a few other things that would probably give away the domain...

g1smd

11:54 pm on Sep 29, 2011 (gmt 0)

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



All of the rules should use RewriteRule. Do not use Redirect or RedirectMatch at all.

The very last rule in the file will be a rewrite to connect extensionless URL requests to the actual file inside the server that will serve the content. This rewrite MUST be listed after all of the redirects.

The www redirect is easy and it will be the last redirect in the list. It will use the [R=301,L] flags.

Before that you'll have your redirect to remove the .php extension. This rule will need to test THE_REQUEST so that it only responds to incoming external URL requests, not previously rewritten internal requests.

Before the .php redirect you'll have the index redirect to completely remove the index filename from the URL request. This rule will need to test THE_REQUEST so that it only responds to incoming external URL requests, not previously rewritten internal requests.

Before all of that, you'll place a ruleset that detects any and all URLs that have one or more upper case characters. This rule will then rewrite (that's a rewrite not a redirect) those requests to a special PHP script. That special PHP script will fix up casing of the URL and fix the www and then send out the 301 header and the location header for the redirect.

We've had this discussion 4 or 5 times so far this month, so there's already plenty more tips and some example code in this forum.

lucy24

1:32 am on Sep 30, 2011 (gmt 0)

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



I can post my existing code replacing the domain but there are a few other things that would probably give away the domain...

If you have something in your htaccess that converts everything to lower-case, I think everyone will happily look the other way, because the consensus was that it can't be done. That is, it's trivial to detect capital letters, but the RegEx dialect used in htaccess doesn't include the \L and \U functions.

Chrispcritters

3:49 am on Sep 30, 2011 (gmt 0)

10+ Year Member



Not htaccess, but apache conf.

g1smd

6:36 am on Sep 30, 2011 (gmt 0)

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



Either way, doing it with a RewriteRule is very inefficient and some code examples I have seen invoke a redirect for each wrong character.

Six wrong characters? User is redirected six times in a chain, each redirect fixing one incorrect character. Avoid this.

The simple "detect if URL has one or more upper case characters, rewrite (that's rewrite, not redirect) this request to a special PHP script, fix the URL in the PHP script, and then PHP script sends 301 header and location header back to browser" solution is the most efficient.