Friendly URL request

Discuss practical ways rearrange URLs using mod_rewrite.

Friendly URL request

Postby dblanco » Wed Apr 08, 2009 1:21 am

I need the Mod_rewrite code to do this:

index.php?zone=val1&syntax=val2

if syntax = "category" &tag=val3 optional &page=val4
if syntax = "search" &query=val3 optional &page=val4
if sytanx = "video" &code=val3&title=val4 none optional
if syntax = <any other value> optional &page=val3


Thanks a lot,

DaViD
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby richardk » Wed Apr 08, 2009 1:02 pm

What is the URL format that you want?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dblanco » Wed Apr 08, 2009 4:19 pm

hi richardk,

well, I have solved the conditional part of my problem. Using this:

Code: Select all
RewriteRule ^(.*)/category/(.*)/(.*)/?$ index.php?zona=$1&syntax=category&tag=$2&page=$3 [L]

RewriteRule ^(.*)/videos/(.*)/(.*)/?$ index.php?zona=$1&syntax=videos&code=$2&title=$3 [L]

etc. for all the possible cases of the variable syntax.


Well, now I need transform to optional the variable page, because currently is required.

for example, if I use this:

mydomain/val1/category/val3/val4 --> works

but if I use:

mydomain/val1/category/val3 --> not works

thanks again,

DaViD

*note: when I don't want to use page parameter is when page = 1
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby richardk » Thu Apr 09, 2009 9:15 am

when I don't want to use page parameter is when page = 1

For mod_rewrite to set page=1 when there is nothing in the URL you will need separate rules
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)/(category)/([^/]+)/?$          /index.php?zone=$1&syntax=$2&tag=$3&page=1    [QSA,L]
RewriteRule ^([^/]+)/(category)/([^/]+)/([0-9]+)/?$ /index.php?zone=$1&syntax=$2&tag=$3&page=$4   [QSA,L]

RewriteRule ^([^/]+)/(search)/([^/]+)/?$            /index.php?zone=$1&syntax=$2&query=$3&page=1  [QSA,L]
RewriteRule ^([^/]+)/(search)/([^/]+)/([0-9]+)/?$   /index.php?zone=$1&syntax=$2&query=$3&page=$4 [QSA,L]

RewriteRule ^([^/]+)/(video)/([^/]+)/([^/]+)/?$     /index.php?zone=$1&syntax=$2&code=$3&title=$4 [QSA,L]

RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/?$            /index.php?zone=$1&syntax=$2&page=$3          [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$                     /index.php?zone=$1&syntax=$2&page=$3          [QSA,L]


If page can be empty instead of 1
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)/(category)/([^/]+)(?:/([0-9]+))?/?$ /index.php?zone=$1&syntax=$2&tag=$3&page=$4   [QSA,L]

RewriteRule ^([^/]+)/(search)/([^/]+)(?:/([0-9]+))?/?$   /index.php?zone=$1&syntax=$2&query=$3&page=$4 [QSA,L]

RewriteRule ^([^/]+)/(video)/([^/]+)/([^/]+)/?$          /index.php?zone=$1&syntax=$2&code=$3&title=$4 [QSA,L]

RewriteRule ^([^/]+)/([^/]+)(?:/([0-9]+))?/?$            /index.php?zone=$1&syntax=$2&page=$3          [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dblanco » Thu Apr 09, 2009 1:04 pm

thanks a lot.

I like the solution of assign by default page=1 when it's empty.

regards
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby dblanco » Sat Apr 11, 2009 6:38 pm

Now, I want to add this rule at your code:

mydomain/news --> index.php?zone=news&page=1

I do this:

Code: Select all
RewriteRule ^([^/]+)/?$ index.php?zone=$1&syntax=news&page=1 [QSA,L]


But so, $_GET['zone'] = "index.php" and it affects too all the other rules.

Thanks again.


PD. Ok, Now I understand it. I must create a rule for each zone variable like:

RewriteRule ^zone1/?$
RewriteRule ^zone2/?$
RewriteRule ^zone3/?$
RewriteRule ^zone4/?$

Thanks !!!
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am

Postby richardk » Sun Apr 12, 2009 12:44 pm

But so, $_GET['zone'] = "index.php" and it affects too all the other rules.

This is because the request to /index.php?zone=news&page=1 also matches that rule.

Either
Code: Select all
RewriteRule ^(zone1|zone2|zone3)/?$ /index.php?zone=$1&syntax=news&page=1 [QSA,L]

or
Code: Select all
# Ignore sub requests (mod_rewrite requesting index.php),
# existing files and existing directories.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?zone=$1&syntax=news&page=1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dblanco » Sat Apr 25, 2009 9:13 am

thanks a lot richardk, you're the best writing mod_rewrite code.

Greetings from Spain.
dblanco
 
Posts: 10
Joined: Wed Apr 08, 2009 1:08 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 22 guests

cron