...

View Full Version : Anyone using apache's ModRewrite? Examples



IdentityCrisis
05-12-2005, 10:34 PM
I'm looking into some information about using ModRewrite to make my dynamic URLS (ie, article.php?S=1&A=5) a little more visually appealing (not to mention, search engine/favourites/bookmark friendly).

I'd like some advice from people who have used ModRewrite before and some examples of how you did it.

Also, if this is in the wrong forum, Mods, please move this topic

Thanks!

IC

brothercake
05-12-2005, 11:23 PM
Check it out:


RewriteRule ^site/([a-zA-Z0-9]+)[/]?([a-zA-Z0-9]+)?[/]? /?c=$1&s=$2 [QSA]

That turns an address like "/site/articles/javascript/" into "?c=articles&s=javascript", and using the "magic" QSA flag (which stands for Query String Append) you can still use CGI parameters at the end of your clean URLs, and they'll get copied seamlessly into the final GET request :thumbsup:

Adding further sections is as simple as adding more replacement expressions - on my site I go up to 5 levels deep:


RewriteRule ^site/([a-zA-Z0-9]+)[/]?([a-zA-Z0-9]+)?[/]?([a-zA-Z0-9]+)?[/]?([a-zA-Z0-9]+)?[/]?([a-zA-Z0-9=&]+)? /?c=$1&s=$2&z=$3&m=$4&$5 [QSA]

Ultimately, using ModRewrite is all about having a good grasp of regular expressions - are you happy with that, or would you like some links?

trancedm
05-12-2005, 11:28 PM
here's a nice and simple tutorial

http://www.oxyscripts.com/item-1052.html

IdentityCrisis
05-13-2005, 04:49 PM
Ultimately, using ModRewrite is all about having a good grasp of regular expressions - are you happy with that, or would you like some links?

I think I'm pretty happy with that explaination. Thanks alot Brothercake!

I am familiar with using regular expressions, although I must admit, I'm out of practice, so it'd take a little bit for me to get back up to speed on them.

Can you give an example of how I would use just numbers instead of words?

Thanks, I really appreciate your insight.

IC

IdentityCrisis
05-13-2005, 05:52 PM
example....

turn domain.com/articles/2/12/4/ into domain.com/article.php?S=2&A=12&SA=4

the final slash I guess would be optional (so it could be articles/2/12/4/ or articles/2/12/4)

Thanks again

IC

IdentityCrisis
05-13-2005, 06:05 PM
Also, do I have to include "RewriteEngine on" before my rewrite conditions?

That might be why it doesn't work...

If you want to just post your entire .htaccess file, that might help! :P

Thanks again

IC

IdentityCrisis
05-13-2005, 10:00 PM
hmm

I seem to have it working sort of... however, the stylesheet isn't loading in.

example..


RewriteRule ^articles[/]?$ /article.php
RewriteRule ^articles/([0-9]+)[/]?$ /article.php?S=$1

If the user types in domain.com/articles the stylesheet loads fine.
if they type in domain.com/articles/ the stylesheet doesn't load.

Also, if they go one step further and type in domain.com/articles/1 (or 1/) the stylesheet also does not load.

Do I need to use absolute addressing for everything now? (ie, do I need to use /images/logo.gif rather than images/logo.gif ?)

Help! Please!

Thanks

IC

brothercake
05-14-2005, 05:05 PM
Do I need to use absolute addressing for everything now? (ie, do I need to use /images/logo.gif rather than images/logo.gif ?)
Well no, but it would be far simpler. Say, for example, your page at "/index.php" gets addressed as "/articles/whatever/" the relative path to "/images/" would then be "../../images/"

Part of the benefit of a dynamic addressing system is that your site topography and folder structure don't have to match - you can organise either independently of the other - so you really do wanna use base-absolute paths or you'll have lost that benefit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum