How can I use .htaccess to shorten URLs?
-
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!
-
Answer:
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
Aquaman at Ask.Metafilter.Com Visit the source
Other answers
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 (http://www.regular-expressions.info/quickstart.html) 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
missmagenta
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?
missmagenta
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?
Aquaman
Thank so much - that did the trick! I did have to disable my redirects to avoid getting sent right back into the deep structure.
Aquaman
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.
TravellingDen
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?
Aquaman
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 http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/. Thanks again for all the help.
Aquaman
Related Q & A:
- How can I use SSL with django?Best solution by Stack Overflow
- How can I use XMP in Sharepoint metadata?Best solution by Stack Overflow
- How can I use mongo-sync?Best solution by github.com
- How can I use real time social data from Datasift and perform real time analytics on it?Best solution by Quora
- How can I use several windows by Ribbon?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.