2012-01-22, 05:50 AM
Rewriting URLs with .htaccess
Approx time for completion: 5 mins.
Today, I found myself wanting to somewhat prettify my URLs. Yes, I am one of those people who cares about little appearance things.
In this post, I am going to show you how to rewrite a URL (like Google SEO does!) with just a few simple steps.
This tutorial may not be for everyone. You might very well not care about appearance as much as me -- and that is just fine.
For those that want a few nicer URLs in their board, keep reading. So, let's begin!
1. Get into your cPanel file manager to edit your .htaccess file. You can also use FTP and edit in your favorite code editor, if you are so inclined.
2. For each rewrite, add somewhere in the file:
RewriteRule ^memberlist memberlist.php
Redirect 302 /memberlist.php http://yourwebsite.com/memberlist
Now in that example, we rewrote the URL slightly for the memberlist. Some may argue that it is an almost pointless change -- but for my tastes, I like it. This example will redirect any requests to httxp://yoursite.com/memberlist.php to httxp://yoursite.com/memberlist . It looks a bit nicer IMO. Rewriting basically points to the exact content in the PHP file, but just "prettifies" the URL a little.I know, out-of-order. But who cares. Let's break down that example.
RewriteRule
tells the RewriteEngine that this is a new rule.^memberlist
defines the faux (nonexistent/fake) folder that we are rewriting tomemberlist.php
tells the RewriteEngine which file we want to apply the rewrite toRedirect 302
tells the server to create a temporary redirect/memberlist.php
the file we are redirecting fromhttp://yoursite.com/memberlist
tells where we are redirecting memberlist.php requests TO.A few points: this code will not work completely verbatim. You may need to turn your RewriteEngine on.
You can change the ^memberlist part to fit what you want the faux directory to be.
You can make this just about anything you want.
Of course, you change memberlist.php to the file that is having its URL rewritten.
DO NOT apply this to search with just this code. search.php uses itself to get the keywords and also to search. I do not currently know how to make it work with optional parameters.
Redirect 302 can be changed to 301 if you wish. 301 is permanent, and your browser will never go to the .php file URL again until you clear your cache and other data. Whereas 302 is temporary, so if you want to change or revert the rewrite, you and your members will not need to clear their cache each time you change it.
Please also note that you may sometimes need to clear your cache to see some rewrites take effect properly.
Also, there are more complex rewrite options, however I have not used them yet due to one issue or another.
To my knowledge, this will only work on Apache-based servers. One exception is LiteSpeed, which, due to it being a drop-in Apache replacement, works just fine with the RewriteEngine.
A few things I have applied this to:
- Game Section (arcade)
- Memberlist
- Newpoints
If you have any questions, ask below. I will try to answer them to the best of my abilities. Admittedly, I only learned this today (knew it existed, but learned how to actually rewrite today).
I have found a bug in this actually - though it took me a long time to notice.
It seems the redirect is on top of the last viewed page. I am not exactly sure why this is happening. My guess is that it has to do with MyBB's outputting of the page. That may be happening before the rewrite. So you might want to hold off on this code.
Support people, or other super-wise minds: If you know the issue and might have a fix, please post it in here. It has to be possible to do without this bug, but I don't want to search through the code to find it
I am now almost willing to bet it is the redirect.