Help me Clean those URLs
December 14, 2006 12:07 PM   Subscribe

How do I make my URLs not need the .php extension?

I am wondering how to make a page, such as:
www.mydomain.com/user_info.php/ID
be able to work in a fashion like
www.mydomain.com/user/ID

How do I go about doing this? I realize that one way I can do it is to create a folder called user, and put an index.php in that folder, but is there a better way to do this, where if someone goes to www.mydomain.com/user it redirects them to user_info.php?
posted by LoopyG to Computers & Internet (16 answers total) 4 users marked this as a favorite
 
mod_rewrite
posted by blueplasticfish at 12:11 PM on December 14, 2006


You might find what you're looking for in How to Succeed with URLs over at A List Apart. It's a bit assumey about how much programming knowledge you have, though.
posted by bcwinters at 12:25 PM on December 14, 2006


Best answer: mod_rewrite. Follow the Meta link at the top of that page for how Wikipedia deals with it.

If you can't confgure mod_rewrite, create an index.php file in every target directory, using header() to redirect the user.
posted by ardgedee at 12:27 PM on December 14, 2006


This tutorial may help.
posted by McSly at 12:41 PM on December 14, 2006


Response by poster: I am hosting my site on Dreamhost. Will I be able to user mod_rewrite (also, I have a fair bit of programming knowledge, but an rather new to PHP/Javascript).
posted by LoopyG at 12:41 PM on December 14, 2006


Best answer: I am hosting my site on Dreamhost. Will I be able to [use] mod_rewrite

yes... mod_rewrite is an apache module, so any apache server has the capability of supporting it. this is what dreamhost uses.

just include your rewrite rules in your .htaccess file... do some googling. this is a pretty common thing in the world of web development, so there is a lot of tutorials and the like out there. it will also help if you brush up on your regular expressions.
posted by blueplasticfish at 12:53 PM on December 14, 2006


mod_rewrite is a known cause of brain explosions. just add this to your .htaccess:

Options +MultiViews

if you have access to your httpd.conf, you can also put this in your directives. this magic i give you is called content negotiation, and the lowdown is here: http://httpd.apache.org/docs/1.3/content-negotiation.html
posted by sonofslim at 1:08 PM on December 14, 2006


also, try using a php framework that has pretty urls already built in, like cake. that can simplify things on so many levels.
posted by blueplasticfish at 1:26 PM on December 14, 2006


MultiViews is the way to go, as sonofslim says. If it works for you it's 100x easier than using mod_rewrite. It's a crying shame it's not enabled by default in Apache.
posted by Khalad at 2:06 PM on December 14, 2006


Response by poster: mod rewrite seems to do the job great! Thanks for the help everyone!
posted by LoopyG at 2:28 PM on December 14, 2006


Response by poster: Another question. How do I rewrite a URL to go to something like:
http://www.mydomain.com/slideshow.php?&date_start=&date_end=&username=20&yC=&xC=&range=&search+

The trouble is, some of those fields (like date_start, etc) can be empty, so I tried a rule like
RewriteRule ^slideshow/([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/?$ slideshow.php?&date_start=$1&date_end=$2&username=$3&yC=$4&xC=$5&range=$6&search+ [L]

but the problem is, that means the URL someone needs to go to is like:
www.mydomain.com/slideshow///20///
to get to the URL i mentioned at the beginning of this comment.

How would I write a rule for such a case?
posted by LoopyG at 3:08 PM on December 14, 2006


LoopyG, you picked the wrong people to listen to. Seriously, throw out mod_rewrite and use content negotiation as sonofslim and Khalad said.
posted by cmiller at 4:01 PM on December 14, 2006


LoopyG: you want "?" which means 0 or 1. e.g. "([0-9]+)/?"
posted by kamelhoecker at 4:04 PM on December 14, 2006


www.mydomain.com/slideshow///20///

you got your query string in my path! no, you got your path in my query string! there are lots of good ways to simplify your urls, but this not one of them. i guarantee this is going to lead to heartbreak and ruin.
posted by sonofslim at 4:19 PM on December 14, 2006


Probably the best way to use mod_rewrite to tidy your URLs is to keep your what-does-a-URL-do logic in your programming language where it belongs and leave mod_rewrite to just direct the relevent bits to it, ala:

RewriteCond %{REQUEST_URI} !^/(images|stylesheets|etc)/
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|index\.php)
RewriteRule . /index.php


Then you poke at $_SERVER['REQUEST_URI'] etc to work out what to do; you can do things like split on /+ and dispatch in bits based on associative array indexes or select () or whatever, and it's easier to intergrate things like URL generation functions so you can map both ways in your code.

Also when you screw up you're less likely to break the webserver with your overcomplex regexps.
posted by Freaky at 8:41 PM on December 14, 2006


I second what Freaky said. To make it even easier, you can use the $_SERVER['PATH_INFO'] to grab just the part of the URL after your script name.
posted by Khalad at 3:51 PM on December 15, 2006


« Older Preparing a course of self-study in Philosophy.   |   Gang hairstyle Newer »
This thread is closed to new comments.