Mod Rewrite URL issue - .htaccess

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]

Related

URL rewrite with PHP and .htaccess

Can anybody advise how I can rewrite the following URL:
www.mydomain.com/products.php?product=product-name
At the moment, it works fine (I use $_GET to grab the unique product name and use it on my page) but I would like to be able to use the following URL format to get the same outcome:
www.mydomain.com/products/product-name
I've seen a few similar examples on here but cannot get them to work with my situation.
This is how your .htaccess will look like,
RewriteEngine On
RewriteRule ^products/([A-Za-z0-9-]+) /products.php?product=$1 [NC,L]
RewriteEngine On this is used to turn on the rewrite engine.
^products/([A-Za-z0-9-]+) matches the URL like (www.mydomain.com/products.php?product=product-name) where product name can be one or more of any combination of letters, numbers and hyphens.
And use that combination of letters in /products.php?product=$1 where $1 denotes the Product name.

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