3

I'm in need of an SEO friendly URL for something small. The current url looks like mysite.com/ny.php?c=[COUNTY]&t=23

With 'c' being a county name, and 't' being the id number of a town.

I found this solution in a previous question:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule ^blog\/([0-9]+)\/.*$ /blog.php?post=$1 [L]
</IfModule>

Which results in /blog/10/any-old-bit-of-text/ from /blog.php?post=10

How could I use something like that, but also have the county in it. So mysite.com/ny.php?c=[COUNTY]&t=23 would become mysite.com/ny/[COUNTY]/23/town-name ?

dgw
  • 13,418
  • 11
  • 56
  • 54
Tim
  • 1,199
  • 1
  • 8
  • 14

2 Answers2

1

Here you go:

RewriteRule ^(ny)/(.+)/(\d+)/(.+)/?$  /$1.php?c=$2&t=$3 [L,QSA]

FYI QSA flag is mandatory as you are changing the query-string.

even in:

RewriteRule ^blog/([0-9]+)/.*$ /blog.php?post=$1 [L,QSA]
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
  • I've never seen the QSA flag in tutorials. Are you sure it is mandatory? – bozdoz Feb 11 '12 at 17:42
  • @bozdoz If you do not know, Do not comment! read first: [Flag QSA](http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa) Apache Docs – ThinkingMonkey Feb 11 '12 at 17:43
  • It is a good idea. It is not "mandatory". So, to your comment... :| – bozdoz Feb 11 '12 at 17:50
  • LOL @bozdoz If his URL does not have any other query string. – ThinkingMonkey Feb 11 '12 at 17:52
  • 1
    Anyway, +1 for suggesting QSA. – bozdoz Feb 11 '12 at 17:59
  • No dice, @ThinkingMonkey! I tried it out, and it doesn't seem to work. Even triple checked to make sure mod_rewrite was working. From everything I've been looking at and dissecting, it definitely seems like that should have worked. I'm not sure if there is any other info that I could provide to help. Other than that `c=` is a county name spelled out (with a capitalized letter) and `t=` is a town id number. – Tim Feb 11 '12 at 21:27
  • 1
    @Tim Are you geting any errors? – ThinkingMonkey Feb 12 '12 at 01:57
  • @ThinkingMonkey I just found the issue! It doesn't like `ny` in there... any thoughts on why? I changed it to `nys` for now. – Tim Feb 12 '12 at 04:28
  • 1
    @Tim Do you have any other rewrite rules? because `ny` should not be a problem. – ThinkingMonkey Feb 12 '12 at 04:36
  • @ThinkingMonkey no sir, I purposely saved my old .htaccess to the side, so I could play with an empty file once I started having problems. With that said, I'm thinking it could likely be something on my host's end, no? – Tim Feb 13 '12 at 05:23
  • @ThinkingMonkey Found it. There must be something on the server prohibiting me from renaming to a directory that has the same name as a page. If I rename `/ny.php` to `/nys.php` it allows me to use `ny` as the renamed directory. MAN was that a waste of time for a simple fix. Thank you so much for the help. Stackoverflow has been a resource for me before, but this is the first time I've asked a question, you've been awesome. – Tim Feb 13 '12 at 05:33
0

If I understand the question you are asking, you want the mod_rewrite code.

I believe it would be something like this:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule ^ny/(.*?)/([0-9]+)/.*$ ny.php?c=$1&t=$2 [L]
</IfModule>

Note: I'm pretty sure you don't need to escape the forward slashes. And if ny.php is in the main directory, it doesn't need a forward slash in front of it.

bozdoz
  • 12,550
  • 7
  • 67
  • 96
  • Too good to be true @bozdoz. It doesn't appear to work. Is there anyway for me to display the URL that it is trying to redirect to? Meaning, i may want it to go to `ny.php/?c=Orange&t=5` and in reality it's attempting to redirect to `ny.php//?c=Orange&t=5` or something like adding a space. – Tim Feb 11 '12 at 21:32
  • Are you receiving an error? What page is it bringing you to? Is there any indication that it is working at all? There could be a few problems. Your server may require that you rewrite the base URL, so you could have to include "RewriteBase /" after turning the engine on. Also, it could be formatted incorrectly. See if your editing program can detect the format of your .htaccess file, and/or change the format. – bozdoz Feb 11 '12 at 23:16
  • Thanks for the reply @bozdoz. I do know that mod_rewrite is working. I tested it with a redirect for a maintenance page. When I put in the "cleaner" url it just 404's on me. – Tim Feb 12 '12 at 00:47
  • So you can rewrite URLs but this particular rewrite rule isn't working? – bozdoz Feb 12 '12 at 01:51
  • 1
    Try logging the errors by putting in these lines of code, found on this site: http://www.easymodrewrite.com/guide-logging – bozdoz Feb 12 '12 at 01:58
  • Found the issue @bozdoz. Any idea why it doesn't like the `ny` ? It now works fine with `nys`. – Tim Feb 12 '12 at 04:29