Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP URL Rewrite

i have a url like this.

http://www.somesite.com/community.php?id=4

for id number 4 would equal Washington for id number 5 would equal Denver for id number 6 would equal New York

I would like the url to be rewritten like this.

http://www.somesite.com/washington  for id = 4
http://www.somesite.com/denver for id = 5

is this possible?

How would I go about doing it?

like image 257
Vibration Of Life Avatar asked Oct 28 '11 14:10

Vibration Of Life


People also ask

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.


1 Answers

Have a look at mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

You can, for example, do it like this:

RewriteEngine on
RewriteRule washington community.php?id=4 [L,QSA]
RewriteRule denver community.php?id=5 [L,QSA]

This would, of course, lead to a LOT of rewrite rules if you have much cities. So, a IMHO smarter way would be to rewrite the URI slug to community.php and lookup the ID from some sort of database:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCOnd %{REQUEST_FILENAME} !-d
RewriteRule /?([a-z-]+) community.php?city=$1 [L,QSA]

HTH

like image 114
ckruse Avatar answered Sep 30 '22 04:09

ckruse