TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Mixed Mod_Rewrite with non (http://www.talkphp.com/advanced-php-programming/1951-mixed-mod_rewrite-non.html)

trs21219 01-14-2008 02:25 AM

Mixed Mod_Rewrite with non
 
ok this is my problem. i hate having to make a new mod rewrite rule everytime i want to add a variable in the url of my websites. i like the way mod rewrite works as it simplify's my visitors browsing and it makes it easier for search engines to find me...

this is what i want. i need a way for my for my pages to use mod rewrite for my components and sub components (act and sub) but then for things like page or article i want to be able to add the old freind ?article= at the end and not have any issues. if you dont get what i mean ill show you below.

Code:

i want this:
www.mysite.com/index.php?act=news&sub=recent&page=2&show=25
to be made into this:
www.mysite.com/news/recent.html?page=2&show=25
using mod rewrite for the act and the sub (or whatever else i put into the mod rewrite page) but still have the ability to just add variables into the url and have them work.

PLEASE HELP!!!

sjaq 01-14-2008 10:47 AM

Code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]

RewriteRule ^([a-z0-9]+)/([a-z0-9]+).html index.php?act=$1&sub=$2 [QSA,L,NC]

for explanation: mod_rewrite cheat-sheet [PDF]

iisbum 01-14-2008 06:09 PM

I think what you want to do is something like this (one of the rewrite rules from sites):

Quote:

RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z]+)/?$ /engine/index.php?section=$1&action=$2&%{QUERY_STRING} [L]
The relevant bit is the %{QUERY_STRING} bit which will pass any variables on the URL into your script.

Mubs

sjaq 01-14-2008 06:29 PM

The [QSA] part at the end of my rule does exactly the same only cleaner...

trs21219 01-15-2008 08:02 AM

thank you both very much. i will make sure to try those and hopefully that will save me alot of hassle ^^:-)

Salathe 01-15-2008 02:15 PM

Just a technical note, the QSA flag appends any query string to the end of the rewritten request in the same way that Mubs' code does. However, a little tweak in the placement of the %{QUERY_STRNG} variable might be of use (or just a fanciful idea).

Consider the following url after rewriting: index.php?section=news&action=recent&<QS> (<QS> is just a placeholder to save space). If the requested url was: /news/recent.html?section=auctions then that request would be rewritten to: index.php?section=news&action=recent&section=aucti ons (sorry vbulletin inserts a space unnecessarily) which would call the recent action within the auctions section, not news!

The tweak to Mubs' rule would be to move the placement of the query string variable:
index.php?%{QUERY_STRING}&section=$1&action=$2

This means that even if section and/or action are passed through the query string manually, the "path" versions will always be used. All that said, it's a minor point and you may choose to simply use the QSA flag and accept the 'false' values in the query string.


All times are GMT. The time now is 08:40 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0