Trying to short this rule without any luck

Discuss practical ways rearrange URLs using mod_rewrite.

Trying to short this rule without any luck

Postby Delusion » Tue Nov 04, 2008 1:29 pm

Hey,

I've made the following rule mainly for SEO.

My url can be one of the 8 options (all redirect same physical file indices_archive.php with different query_string):
  • http://example.com/indices
  • http://example.com/indices/
  • http://example.com/indices/reviews
  • http://example.com/indices/reviews/
  • http://example.com/indices/reviews/1
  • http://example.com/indices/reviews/1/
  • http://example.com/indices/reviews/1/GTA
  • http://example.com/indices/reviews/1/GTA/


The code I made to apply the rule:

Code: Select all
RewriteRule ^indices/([^/\.]+)$ indices_archive.php?type=$1 [L]
RewriteRule ^indices/([^/\.]+)/$ indices_archive.php?type=$1 [L]
RewriteRule ^indices/([^/\.]+)/([^/\.]+)$ indices_archive.php?type=$1&page=$2 [L]
RewriteRule ^indices/([^/\.]+)/([^/\.]+)/$ indices_archive.php?type=$1&page=$2 [L]
RewriteRule ^indices/([^/\.]+)/([^/\.]+)/([^/\.]+)$ indices_archive.php?type=$1&page=$2&q=$3 [L]
RewriteRule ^indices/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ indices_archive.php?type=$1&page=$2&q=$3 [L]


Tt works but seems inefficient and long for no reason, some of the fields are optional and including them in a different row seems wrong.

I'm trying to simplify my rule into less rows without any luck.


Thanks in advance,
Oleg
Delusion
 
Posts: 2
Joined: Tue Nov 04, 2008 1:18 pm

Postby richardk » Wed Nov 05, 2008 1:37 pm

/? for an optional slash
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^indices/([^/]+)/?$                 /indices_archive.php?type=$1              [L]
RewriteRule ^indices/([^/]+)/([^/]+)/?$         /indices_archive.php?type=$1&page=$2      [L]
RewriteRule ^indices/([^/]+)/([^/]+)/([^/]+)/?$ /indices_archive.php?type=$1&page=$2&q=$3 [L]


Or one rule with (?:optional)?
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^indices/([^/]+)(?:/([^/]+)(?:/([^/]+))?)?/?$ /indices_archive.php?type=$1&page=$2&q=$3 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Delusion » Wed Nov 05, 2008 3:30 pm

richardk, thanks :)))

works like a charm
Delusion
 
Posts: 2
Joined: Tue Nov 04, 2008 1:18 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 5 guests

cron