Join 3,415 readers in helping fund MetaFilter (Hide)


How can I use .htaccess to shorten URLs?
February 23, 2010 3:15 PM   Subscribe

I need some specific help with my .htaccess file. I'm new to the Rewrite module and can't quite figure out what I need to do to serve my pages (which live inside some nested directories) up with a nice short URL.

So I've got all my new web pages in a directory a few levels deep, i.e.:

domain.com/newstuff/pages/page_title.php

and I'd like their URLs to display in the browser address bar as:

domain.com/page_title.php

Also, I'd like users (and my internal links) to be able to use the short form as well. I've already got redirects set up to forward the latter to the former, but the URL reflects that (showing the long form in the address bar), and I'd like it to be nice & short, consistently. Any advice would be greatly appreciated!
posted by Aquaman to Computers & Internet (8 answers total) 4 users marked this as a favorite
 
so all your pages live in /newstuff/pages?

Try this


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ newstuff/pages/$1 [QSA,L]

I'm a little rusty so you might need a / infront of newstuff
posted by missmagenta at 3:45 PM on February 23, 2010


Sorry, I see that you say you already have a redirect working, its just the url that you're having a problem with - what flags are you using? (the parts in the []) Are you using R?
posted by missmagenta at 3:46 PM on February 23, 2010


The Redirects work fine, but send the browser to the long URL. I haven't tried Rewrites yet at all, having just learnt about them. Can you break down your approach for me a little more?
posted by Aquaman at 3:56 PM on February 23, 2010


Sorry - I missed a line, if its not there already you'll need

RewriteEngine On


I'm not great at explaining these things but I'll try

The first 2 lines basically say not to apply the rule to files and directories that exist

The rule is in 3 parts

^(.*)$ - is a regular expression that captures the whole url in a block (later referred to as $1)

^ and $ are special characters that basically mean beginning and end (more info)
The Brackets simply capture the group so we can reference it later
The . matches any character and the * says to match 0 or more times

The second part is the url you want to fetch instead of the requested one which in this case is the new path plus the group we captured in the first part (in this case, its the requested page) so in a request for page_title.php $1 holds page_title.php and newstuff/pages/page_title.php is requested

The last bit is the flags:
QSA means Query String Append - which passes the query string through to the new page
L means Last rule - meaning don't continue to check this request against any other rewrite rules
posted by missmagenta at 4:12 PM on February 23, 2010 [1 favorite]


Thank so much - that did the trick!

I did have to disable my redirects to avoid getting sent right back into the deep structure.
posted by Aquaman at 4:38 PM on February 23, 2010


Just in case it helps - keep in mind there is a RewriteLog option (might be RewirteLogFile) and a debug level that can be set - you don't want it for production, but setting it to, like, 5 if things are misbehaving might help figure out what's up.

Second..... ready very, very carefully the Apache rewrite guides.... They seem wordy and techy, but rewrite-rules don't always work exactly the way you may expect them to.

Eg: Although you place RewriteCond statements before RewriteRules - the parser actually processes each incoming URL for a valid maching RewriteRule, and then checks the relevant RewriteCond statements.

And just as a matter of principle - how about trying to go all the way and get rid of the .php extension altogether, and do it all in rewrites? Makes for cleaner looking URLs and lets you migrate to other platforms later.
posted by TravellingDen at 6:18 PM on February 23, 2010


Thanks, TD, I will check on the documentation more thoroughly. I'm not sure what you mean by "get rid of the .php extension altogether"; the files being called are mostly text files full of php include statements, used to build each page.

Any further advice on how to get non-existing url requests to parse as 404 errors instead of 500 errors now?
posted by Aquaman at 6:38 PM on February 23, 2010


Followup: everything is working great, I'm brushing up on my regex, and I was also able to build a hotlink script that plays the sad trombone sound whenever anyone tries to illicitly nab an mp3. Ha ha!

The best htaccess resource I found is an awesomely detailed (but not opaque) guide called Stupid Htaccess Tricks. Thanks again for all the help.
posted by Aquaman at 1:07 PM on February 25, 2010


« Older Cordless electric lawn mower r...   |  Struggling with Wordpress, to ... Newer »
This thread is closed to new comments.