.htaccess and seo friendly search query string - .htaccess

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.

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]

Related

.htaccess rewrite to add query parameter

I need to modify all requests bearing the form
http://example.com/dw2/dokuwiki/doku.php/page to
http://example.com/dw2/dokuwiki/doku.php/page?do=export_xhtml
The page bit is variable - it corresponds to each paage in the wiki. I should mention that given the way dokuwiki syntax works page could contain one or more colons. e.g. glossary:archive.
The intent here is to extract the bare page content (shorn of the header, sidebar etc) of the wiki for distribution via a CDN. This does not give a complete solution since dokuwiki still leaves in a lot of unrequired verbiage in the exported markup file but gets me most of the way there. I'd much appreciate any help with this.
Place this rule as your very first rule in /dw2/dokuwiki/.htaccess:
RewriteEngine On
RewriteBase /dw2/dokuwiki/
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(doku\.php/[^/]+)/?$ $1?do=export_xhtml [L,NC,QSA,R,NE]

htaccess RewriteRule - Multiple optional parameters in no particular order

I need friendly urls on my webpage and I'm using the htaccess file for that purpose.
Actually it's pretty easy, but there are some pages where the url can have multiple optional parameters in no particular order and I can't image how the rewrite rule shpuld look like for those pages.
For example, a page to display a huge list of movies from the database. You can filter by medium (dvd, blu-ray), age, genre. You can sort the results by alphabet, rating, etc. and you can switch pages. All of this works with url paremeters.
index.php?movies
Shows all movies, no filters, no sorting. The rewritten url should lie this:
/movie
No problem so far. But it can also look like this:
index.php?movies&medium=1&genre=3&age=17&sort=alphabet&page=15
This would return page 15 of all R-rated horror movies on dvd, ordered by name.
The parameters have no particular order and are all optional. If my RewriteRule would loo like this
RewriteRule ^movies/([0-9]+)/([0-9]+)/?$
how do you know if the first parameter is medium or page or whatever?
Is there a way to identify the parameters?
/movies/genre-3/page-15
Thank you very much!
You can use a rule like this:
RewriteEngine On
RewriteBase /
# reucrsion based rule to convert /n1/v1/n2/v2 to /?n2=v2&n1=v1
RewriteRule "([^/]+)/([^/]*)(/.*)?$" $3?$1=$2 [L,QSA]
This rewrites a URI like /movies/medium/1/genre/3&age/17/sort/alphabet/page/15 to:/?page=15&sort=alphabet&3&age=17&1=genre&movies=medium

Mod Rewrite URL issue

I need to build SEO friendly URLs, I have this URL:
http://www.misite.com?search=&area=$1&cityid=$1&lang=es&subcatid=66&view=ads&catid=9
I need to make them look like this SEO friendly URL:
http://www.misite.com/sale-houses-california
So far, I've added this code to htaccess this:
RewriteRule ^sale-houses-([-]?[a-z]+)? ?search=&area=$1&cityid=$1&lang=es&subcatid=66&view=ads&catid=9 [QSA]
I have cities, categories and subcategories combining all so I need an easy solution for all of the combinations
I don't know the correct expression: ([-]?[0-9]+)? / ([-]?[a-z]+) or similar.
The URL structure is ^text-text-text in lower case
Is it possible to make something more general like this (see below) or do I have to write a line per each category?
RewriteRule ^([-]?[a-z]+)-([-]?[a-z]+)-([-]?[a-z]+)? ?search=&área=$1&cityid=$1&lang=es&subcatid=$1&view=ads&catid=$1 [QSA]

htaccess to rewrite param=NUMBER into /text

I am trying to figure out how to rewrite URLs from something like this:
example.com/collection.php?collection=1
Into:
example.com/collection-name.php
I recently redesigned an e-commerce site and need to redirect the old URLs to the new ones. I've seen loads of instructions on how to use the value of collection=1 in a rewritten URL but not how to use text instead of the value. There are only 5 collection values that need to be redirected but in addition there are also old URLs that have multiple params in them that also need to be rewritten/redirected as text. I hop that made sense.
So I think I can get them all worked out if I can get the initial redirect set up.
Other/more complex old URLs look like this:
example.com/collection.php?collection=1&product=2&item=3
Which would then need to be redirected to:
example.com/collection-name/sub-collection-name/product-name.php
Not sure exactly how to go about doing this. I don't want to write line after line in the .htaccess file but I have no idea of how else to accomplish this.
Thanks in advance, I appreciate andy and all help in this matter!
EDIT------------------------------------
Here's my new rewrite condition and rule based on the info provided to me. This would be the rule for the URLs containing all the query params using 3 separate RewriteMaps.
RewriteCond %{QUERY_STRING} collection=([^&]+)&product=([^&]+)&item=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}/${rangemap:%2}/${productmap:%3}\.php [R=301,L]
Please let me know if anything looks off or I missed anything. Wasn;t sure if I needed to use $1, $2, $3 for each of the query params. I'm still a NOOB with rewrites. Thanks!
Edit------------------------------------------------------
Using the following code in my .htaccess file:
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}\.php [R=301,L]
My URLs that start as "example.com/collection.php?collection=1
Are being rewritten as: example.com/shop/.php?collection=1
I am a bit lost on this one. Could it be that my Redirect Maps are not being read? Or is it something else? Thanks for the help.
You want to look into using a RewriteMap. You can specify the maps you want for collection, sub-collection and products, and then look up the text for each number.
So you would define a RewriteMap in your virtualhost config with something like
RewriteMap categmap txt:/path/to/category/map.txt
Then your rewrite rule would look something like
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php ${categmap:%1} [L,R]
Add more RewriteConds for your more complicated cases and make separate rules out of them. Place the more specific rules first in the file, then the more general ones.

301 Redirect to change structure

I have been researching redirects for a few days now and am still struggling, so I decided to post my first question here. For some reason, it is just not clicking for me.
I have redesigned and developed a client's WordPress site and need to update it's structure.
The site's current structure is:
www.domain.com/blog/postname/2011/12/26/
The new structure should be:
www.domain.com/blog/postname
I really thought this was going to be easy since all I am looking to do is drop the date, but have not been able to grasp the whole wildcard aspect and how to end what I am trying to match. Any help would be greatly appreciated. A simple answer is great, but an explanation would be even better.
I am assuming you already know how to change your WordPress permalink structure to drop the date.
To 301 redirect all of the old URLs to the new ones, add the following rules to your .htaccess file in the root of your websites domain, ahead of any existing rules that are there.
#if these 2 lines already exist, skip them and add the rest
RewriteEngine on
RewriteBase /
# if there is a request of the form /blog/post-name/yyyy/mm/dd/
RewriteCond %{REQUEST_URI} ^(/blog/[^/]+/)[0-9]{4}/[0-9]{2}/[0-9]{2}/$ [NC]
#redirect the request to the URL without the date
RewriteRule . %1 [L,R=301]
If you want to learn more about .htaccess/rewriting you can take a look at the following urls: Indepth htaccess, Brief Introduction to Rewriting, Apache Mod_rewrite.
Let me know if this works for you and/or you have any issues.

Resources