Is it possible to hide QSA variables when using mod rewirite - .htaccess

My rule in the htaccess file
RewriteRule ^brands/([^/.]+)/?$ browse-brand.php?brand=$1 [L,QSA]
Turns the midly ugly
www.mysite.com/brands.php?brand=$brandName
into a more attractive
www.mysite.com/brands/brandName/
Each brand page is used to display hundreds of products relating to that brand so is split in to pages and can be filtered to show 20/50/100 products or order by price/bestsellers etc
So my newly made 'pretty urls' become ugly again when attaching the sorting and ordering variables using QSA, such as:
www.mysite.com/brands/brandName/product_show=20&product_sort=sortpricedesc&page=2
Obviously the pages won't perform without these extra variables. I suppose it's possible to change my RewriteRule to include these, like so:
www.mysite.com/brands/brandName/20/sortpricedesc/2/
but that's still a bit ugly.
Is there a way to hide these variables from displaying in the URL bar? How do you deal with product catalogues with multiple pages?

You can use this .htaccess:
RewriteRule ^brands/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ browse-brand.php?brand=$1&product_show=$2&product_sort=$3&page=$4 [L,QSA]
RewriteRule ^brands/([^/.]+)/?$ browse-brand.php?brand=$1 [L,QSA]
The final page is the same browse-brand.phpwith new variables. But you also can change the name in this case.

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

Static URL rewrite with htaccess hide full URL path and .html

My url is ukneurology.com and the url for one specific program is http://ukneurology.com/html/clinicInfo/stroke.html. I want to change the second URL to ukneurology.com/stroke along with every other page (they will all have a different path). I have seen a lot of dynamic pages, but none of my page URLs look like what I have seen in other examples. I just need an htaccess file where I can type in the real URL and then type in what I want it to look like in the browser and what people can use. Is there a template I can use? Or even better, a generator that will do it for me? I can't find anything!
You can use something like this: RewriteRule newPageName oldPageName [L] Pay attention: you have to escape . in the newPageName like this: \.. NewPageName and oldPageName don't include your domain.
In your example this would be right: RewriteRule stroke html/clinicInfo/stroke.html [L]
If the user tries to access http://ukneurology.com/stroke, he will see the page http://ukneurology.com/html/clinicInfo/stroke.html without a redirect (so the URL stays the same). The [L] avoids that any other RewriteRule will be met.
If you have a special scheme (for example http://ukneurology.com/abc => http://ukneurology.com/html/clinicInfo/abc.html), you can use a regex to avoid writing thousands of RewriteRules by hand: RewriteRule ([^/]+) html/clinicInfo/$1.html [L]

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.

.htaccess and seo friendly search query string

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]

Resources