htaccess RewriteRule for optional Parameter? - .htaccess

I am currently trying to implement a REST API. For this, the URL to be called should be in a specific format:
/customer/{customerid}/orders/periods
/customer/{customerid}/orders/periods/{period}
I got variants 1 and 2 working with this RewriteRule.
RewriteRule ^customer/([a-zA-Z0-9-]+)/orders/periods/([a-z0-9-]+)?$ api/customer.php?customer_id=$1&period=$2 [L]
Now the parameter "year" should be added optionally:
3. /customer/{customerid}/orders/periods?year={year}
My approach would be this rule, but that doesn't really work (at least not for variant 2).
RewriteRule ^customer/([a-zA-Z0-9-]+)/orders/periods([a-zA-Z0-9-]+)?$ api/customer.php?customer_id=$1&year=$2 [L]
Does anyone have any ideas? My knowledge of Rewrite Rules is unfortunately somewhat limited.
Thanks in advance.

Related

.htaccess and differentiating between query strings by case

I wonder if someone can advise please?
After migrating an old php site to Wordpress, I converted some 450+ old pages to static html and used the following very satisfactorily;
RewriteEngine On
RewriteCond %{QUERY_STRING} ID=([0-9]+) [NC]
RewriteRule (.*) {domain}/{filename}%1.html? [R=301,L]
However, a handy search plug-in also uses "id" [lower case] in its URLs and these are being routed to the static page with the same numerical id.
Is there a way of detecting uppercase 'ID' only for the redirect and ignore lower case? I appreciate it is poor coding, but I am unable to change either Query String names.
Sincere thanks for any guidance!

Rewrite Rule - add not precised number of parameters to a rule

is it possible to make rule that works like this:
example.com/param1val/param2val/param3val/...
And as follow, many params. I can't know how many of them would be there, but I want to rewrite it to:
example.com/index.php?param1=param1val&param2=param2val&param3=param3val...
and so on. Is it possible in one rule?
As per my comment this answer is for case 1):
RewriteEngine On
## reucrsion based rule to convert /user/n1/v1/n2/v2 to /user.php?n2=v2&n1=v1
RewriteRule ^(index\.php)/([^/]*)/([^/]*)(/.*)?$ /$1/$4?$2=$3 [L,QSA,NC]

Mod rewrite with query strings after .html

Im trying to perform a mod rewriting to my web site. And I used to do this rewriting for quite some time. Of course I am very much upto the task until I met new requirement to create a rewritten URL with query strings after .html
Example - http://kypseli/admin/catagory.html?parent_id=1
I have used this this regex.
RewriteRule ^([A-Za-z0-9-/_]+).([html])/?$ index.php?rt=$1&%{QUERY_STRING}
But the query string value (parent_id) not passing.
This is working with out the .html part for perfection.
http://kypseli/admin/catagory/?parent_id=1
But I want it with .html part, as I have found lots and lots of same technique usage every where in WWW. here is one example
http://www.espncricinfo.com/ci/content/video_audio/559158.html?genre=46
Can someone please help.
If you want to get all query string elements from the first query to be appended on your rewritten query you need to add the [QSA] flag to the rewriteRule, or [qsappend] if you prefer long and descriptive tags.
RewriteRule ^([A-Za-z0-9-/_]+)/?$ index.php?rt=$1 [qsappend]
Now I do not understand fully what you mean about '.html', but I hope this will be enough.
EDIT
your problem is maybe the dot, it should be escaped \. and it should be in the optionnal part if it is optionall so :
RewriteRule ^([A-Za-z0-9-/_]+)/?$ index.php?rt=$1 [qsappend]
RewriteRule ^([A-Za-z0-9-/_]+)\.html?$ index.php?rt=$1 [qsappend]
Or if you want only one rule, this should work (untested)
RewriteRule ^([A-Za-z0-9-/_]+)([\.html|/])?$ index.php?rt=$1 [qsappend]
And it's unsure you really need the ? in your rewriteRule, you may want to apply it even without query strings.

Time-Dependent Rewriting and single digit TIME_MON/TIME_DAY variables

I am trying to redirect access to one part of my site before a certain date. I am using ISAPI_Rewrite 3 on IIS 6 but I believe ISAPI_Rewrite is compatible with mod_rewrite syntax. Imagine I wanted to redirect pages to a certain address before the 1st April 2012. I have found examples where people use something like this:
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} <20120401
The problem I have is that the server variables for TIME_MON and TIME_DAY appear to be returning single digits (e.g. 1 rather than 01).
So for the 1st May %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} returns 201251 rather than 20120501.
201251 is less than 20120401 BUT the first of May is AFTER the first of April.
How do I get around this problem?
Looking at Time based rewrites - how to handle two seperate years? I have realised that I may be coming at this from the wrong direction.
I think something like the following could work for me:
RewriteCond %{REQUEST_URI} ^/Someplace/(.*)\.aspx$
RewriteCond %{TIME_YEAR} ^2012
RewriteCond %{TIME_MON} <4
RewriteRule ^(.*)$ /Someplace/systemdown.html [R=301,L]

.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