Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite

         

Slingshot

1:04 am on May 9, 2008 (gmt 0)

10+ Year Member



I'm trying to make this URL:
mydomain.com/city.php?id=California&id2=San-Diego-County

Look like this:
mydomain.com/California/San-Diego-County.html

Here's what I have in my htaccess file:

RewriteEngine on

RewriteRule ^([^/]*)\.html$ /county.php?id=$1 [L]
RewriteRule ^id/([^/]*)/id2/([^/]*)\.html$ /city.php?id=$1&id2=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /company.php?id=$1&id2=$2&id3=$3 [L]

Doesn't seem to work. Can anyone see any obvious errors?

Thanks

jdMorgan

1:47 am on May 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rules will rewrite URLs like mydomain.com/California/San-Diego-County.html, when requested by a client (.e.g. browser or search engine robot), to your scripts such as "county.php" so that content can be generated.

But mod_rewrite does not "change" URLs; It merely says, "When a client requests '/California/San-Diego-County.html', pass the request to the content.php script, and pass the 'id' to it so that it can generate the proper HTML page."

You will need to change the links on all of your pages to reference the "/California/San-Diego-County.html"-style URLs in order to "change" those URLs.

If your question really has to do with rewriting rather than changing URLs, then I'd suggest a review of the regular-expressions tutorial cited in our forum charter; The patterns in your rules are just "not quite right-looking" but because you gave only one example URL-path and no description of how you want it to work, it's impossible to know what to recommend. For example, I think I'd recommend changing your first rule to


RewriteRule ^[^/]+/([^.]+)\.html$ /county.php?id=$1 [L]

if you want to drop "California" and pass "San-Diego-County" to the "county.php" script, but I can't be sure of that or the other rules without a full description of your intent.

Also, I might as well warn you that you'll probably be happier in the long run if you do not mix upper- and lower-case letters in your URLs, as it is very difficult to fix mixed-case problems in .htaccess; By mixing case, you are almost guaranteed to have other Webmasters, bloggers, and forum posters making mistakes in linking to your pages. Apache, unlike IIS, is case-sensitive.

This is good because it prevents the massive duplicate-content problems (and possible search ranking penalties) inherent in a case-insensitive server, but it means that any request with a case error will go 404-Not Found on an Apache server unless you handle it and "fix-up" the requested URL. When doing so, it is far easier to force either all-lowercase, or all-uppercase. Mixed-casing is a nightmare, and pretty much has to be handled one URL at a time. That can quickly make your .htaccess file huge, and therefore, make your server very slow.

Jim

Slingshot

3:15 am on May 9, 2008 (gmt 0)

10+ Year Member



Thanks for replying Jim.

So if I type this in my browser "mydomain.com/California/San-Diego-County.html" it should display the same thing as "mydomain.com/city.php?id=California&id2=San-Diego-County"? It gives me a 404.

All I want to do is create a cleaner URL. This is for a PPC test, not worried about organic rankings for this site. Ultimately, it probably doesn't matter for the test.

I'm fairly new to this side of things, trying to learn and expand my knowledge.

Slingshot

3:32 am on May 9, 2008 (gmt 0)

10+ Year Member



Ok, it just started working for some reason.

When I type "mydomain.com/California/San-Diego-County.html" into the browser it pulls the correct data.

Again, thank you Jim.

[edited by: Slingshot at 3:32 am (utc) on May 9, 2008]

jdMorgan

3:44 am on May 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mydomain.com/California/San-Diego-County.html --> mydomain.com/city.php?id=California&id2=San-Diego-County
would be:

RewriteRule ^([^/]+)/([^./]+)\.html$ /city.php?id=$1&id2=$2 [L]

So I'm gonna guess that your whole "rule stack" should be:

RewriteRule ^([^./]+)\.html$ /county.php?id=$1 [L]
RewriteRule ^([^/]+)/([^./]+)\.html$ /city.php?id=$1&id2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ /company.php?id=$1&id2=$2&id3=$3 [L]

Jim

g1smd

8:14 pm on May 9, 2008 (gmt 0)

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



I can't stress enough the point about mixed case URLs, and doing your best to avoid them.

If you can go all-lower-case you will have a very much easier time in the long run.