1

I am really new to .htaccess. I was wondering how to create a complex seo friendly urls for the search string and selected filters.

I'm using following code currently for the search.

ReWriteRule ^search/(.*)/(.*)   ?module=search&q=$1&page=$2 [L]
ReWriteRule ^search/(.*)        ?module=search&q=$1 [L]

When it comes to adding filter options it starts to be a nightmare. Consider following filters;

Short by: none, name, date + ASC, DESC. Category: none, category ID. Search In: All, title, Message, Author.

When all filters are selected our address would be;

Raw : www.site.com/?module=search&q=test&shortBy=name_ASC&catID=1&searchIn=title

Seo Friendly : www.site.com/search/test/name_ASC/1/title/

Now that's not complex at all but I just want to understand the how things work. Do I have to apply all the filters to URL even if they are not selected? This would create longer URLs for no reason so I believe there must be another approach to this.

How do I define the rule in .htaccess? As in my example, I am writing at least 2 rules in my .htaccess file (2nd one is for pagination).

All I could think right now to do something like this ^search/(.*)/(.*) ?module=search&$1=$2 [L] but this doesn't look elegant.

I will be glad if you could help me out with this problem. I will be grateful if you could also share some of your experiences to a .htaccess newbie like I am.

1
  • Is there a page behind this e.g. index.php that the request is submitted to? Nov 24, 2011 at 14:54

1 Answer 1

4

Put the rules below at the top of your .htaccess file (in the root directory of your site) and then select one of the options below, modify it to your needs and place in after in your .htaccess file.

The first option will match the most urls and is not recommended. The second will only match urls with 5 directories, but requires all the paramters, so I recommend the 3rd.

Though the pattern below may not look elegant, it is a featured solution in The Definitive Guide to Apache mod_rewrite

For more details look at the apache docs or some examples or this post

In all cases, I am assuming that the requests go to index.php, so you should modify this to match the actual page.

This section should go at top of .htaccess file for all 3 options

#for all 3 options these 2 lines should be at the top of the .htaccess file
RewriteEngine On
RewriteBase /

Option1

#1 if all parameters are optional, and you page (index.php) can handle blank parameters use
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]

Option2

#2 if all parameters are required use
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?([^/]+)/([^/]+)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]

Option3

#3 if the module is required and other parameters are optional, then this is better
RewriteCond %{REQUEST_URI} ^/(search|produce_detail|other_modules_here)/
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=%1&q=$1&shortBy=$2&catID=$3&searchIn=$4 [L]
1
  • The problem is this is not the only page. I would like to go with option 1. I can handle blank parameters but I definately wouldn't like to see them on URL like = search/query//4/news with option 1 if this is the case, it just doesn't look elegant. I will give it a try thank you very much.
    – Revenant
    Nov 25, 2011 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.