.htaccess Rewriting
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.
tells the RewriteEngine that this is a new rule.
defines the faux (nonexistent/fake) folder that we are rewriting to
tells the RewriteEngine which file we want to apply the rewrite to
Redirect 302
tells the server to create a temporary redirect
the file we are redirecting from
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
Hope I helped you out today. If you have any feedback or extensions to this tut, then let me know and I will add them if they are necessary/worthwhile.

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 Wink
I am now almost willing to bet it is the redirect.
PGP Key (Fingerprint: 23B6 F4C0 FE2D 45AA 61A0 1E86 DB87 09DC DD87 6E40)
awesome!!! Big Grin
Exclamation MyBB-Es.Com, spanish support for MyBB Wink
I am going to see what happens without the redirect right now.
My guess is that if you rewrite and change your links that it will work just fine. Because we know rewriting works, more than likely that my methods were wrong. Again, it took me like 4 hours to notice Big Grin
Edit: I have confirmed that this was an issue with the redirect. I will update the original post momentarily (once I finish making my URLs pretty again!)...
Edit: Or not. Investigating the issue further now.
EDIT3: I am beginning to wonder if it is a glitch on my iPad - because the issue is persisting after a hard refresh.

Could someone please test http://gamervoid.com/memberlist and report any findings.

EDIT4: It seems to be an issue with applying this to pm.php . Once I remove that rule, it works even with redirects.
PGP Key (Fingerprint: 23B6 F4C0 FE2D 45AA 61A0 1E86 DB87 09DC DD87 6E40)
^ Addition to above post: IIRC, the same issue exists when applying a Rewrite to the UserCP page.
Plus "BUMP"
PGP Key (Fingerprint: 23B6 F4C0 FE2D 45AA 61A0 1E86 DB87 09DC DD87 6E40)
Is it possible that you change this for the default rewrite rules?

So it is like forum.com/forumname/topicname ?
I use the following on my site

	RewriteRule ^legal\.html$ /board/misc.php?page=tos [L,QSA]

You can even rewrite pages with forms, but to maintain the URL scheme you are rewriting to, you usually need to edit the file so the the form's action attribute uses the new URL
Lost interest, sold my sites, will browse here once in a while. It's been fun.
Sorry for the delayed response Serial Thrilla. You can achieve that effect with Google SEO, and turning on its parent forum setting. I don't know EXACTLY how right now.

@Pavemen - I will have to try some more on my board tonight. Thinking about applying it to the Help Documents or misc.php .Wink
PGP Key (Fingerprint: 23B6 F4C0 FE2D 45AA 61A0 1E86 DB87 09DC DD87 6E40)
one question


can i rewrite above url to




with htacces if yes how?

thanks in advance

Forum Jump:

Users browsing this thread: 1 Guest(s)