htaccess RewriteRule - Multiple optional parameters in no particular order - .htaccess

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

Related

.htaccess dynamic links rewrite engine

I need your help creating some links using mod_rewrite.
I have some pages like:
register.php
login.php
And have the code for them:
RewriteRule ^register/?$ register.php [NC,L]
RewriteRule ^login/?$ login.php [NC,L]
My problem is with "dynamic" links I have since I can't get them working.
For exemple I have links like:
index.php?id=news
índex.php?id=news&article=2
How can I transform those links into:
/news/
/news/article_name
And I have some products (that could have the same name in the same category) but with different ID's like:
índex.php?id=products&p=30
How can I change it to
/products/product-name
After this, is it possible to "generate" an unique name? Since I would like not set in the link the unique ID like products/45342/product-name?
What are the changes I need to make to my code to work with those links?
For example I have links like:
To clarify, you must first change the links in your application to be of the form /news/ or /news/article_name (but see below). You then rewrite these "pretty" URLs back to the underlying filesystem path.
So, to rewrite /news/ back to index.php?id=news you can do something like:
RewriteRule ^(news)/$ index.php?id=$1 [L]
Using the $1 backreference just saves typing. Only use the NC flag if this must be a case-sensitive match, but note that this potentially creates duplicate content, so you must specify the canonical URL in some other way (eg. rel="canonical" link element). For the same reason, only make the trailing slash optional if this is a specific requirement.
However, it's not possible to rewrite /news/article_name back to index.php?id=news&article=2 (I assume that should be i, and not í, as in your question?) since the article ID (ie. 2) is not present in the source URL. You need to include the ID in the source URL (or make the article_name unique and a key in your lookup). It would be more usual to create a URL like /news/2/article_name (which is what StackOverflow does), which can be easily rewritten. The article_name in the URL is purely for users (and indirect SEO). In which case you could rewrite this like so:
RewriteRule ^(news)/(\d+)/ index.php?id=$1&article=$2 [L]
This will rewrite /news/N/<anything> to /index.php?id=news&article=N (where N is 1 or more digits).
However, since it rewrites <anything> you should also implement a redirect in your application when the non-canonical article_name is accessed. (Which again, is what StackOverflow does.)
And I have some products (that could have the same name in the same category) but with different ID's like: índex.php?id=products&p=30
How can I change it to /products/product-name
The same principle as mentioned above applies here also.
After this, is possible to "generate" an unique name?
You can generate this "unique name" in your application, not .htaccess. Build you URLs in your application etc.
Since I would like not set in the link the unique ID like "products/45342/product-name" ?
As mentioned above, either your product-name is unique, and behaves like your id. Or you incorporate the unique ID in the URL - this is the far more common approach, offers greatest flexibility and is less prone to error. A "short" URL like /products/45342 will redirect you to the correct canonical URL.

htaccess - get content on one url from another url

Is there the opportunity to get content on one url from another url? like this:
test.com/dir1 shows content from query test.com/getinfo/index.php?q=dir1
or
test.com/dir2 shows content from query test.com/getinfo.php?q=dir2
I need that because i must do 50 urls like test.com/dir1 ../dir50 which has the same template but with some different content based on query and one virtual url with query is better than 50 real directories.
I tried do but couldnt! Thanks in advance!
If it is all on the same host, then have a look at mod_rewrite, which can do amazing things. Just make sure you read the manual through, as there are a lot of details to take in.
RewriteEngine On
RewriteRule ^/dir1$ /getinfo/index.php?q=dir1 [L]
or more generically:
RewriteEngine On
RewriteRule ^/dir(.*)$ /getinfo/index.php?q=dir$1 [L]

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.

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 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