Make optional parameter with .htaccess - apache

I have looked for the answer to my problem here, but the solution is always provided without explaining how to do it, that's the reason I can not do it properly.
I have this code:
RewriteRule ^dex/([^_]*)/([^_]*)/([^_]*)/([^_]*)/([^_]*)$ /dex.php?one=$1&two=$2&three=$3&four=$4&five=$5 [L]
This htaccess as it is makes it mandatory that all parameters are given. This URL works:
http://example.com/dex/one/two/three/four/five
I also want to make it work like this:
http://example.com/dex/one/two/three/four
Making the last (or some) parameters optional. I read something about QSA|qsappend here: http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsa but I can't understand it completely.
Any help? Thank you

To anyone having the same issue, this is the code used to fix it:
RewriteRule ^dex/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?([^/]*)?$ /dex.php?one=$1&two=$2&three=$3&four=$4&five=$5 [L]
Changed ([^_]*) to ([^/]*) and added ? after what I wanted to make optional.
In this case: /? is making a end slash optional, and ([^/]*)? is making the last parameter optional. So it works when the URL is like this:
http://example.com/dex/one/two/three/four/
http://example.com/dex/one/two/three/four
http://example.com/dex/one/two/three/four/five
http://example.com/dex/one/two/three/four/five/
Hope this helps someone.

Rewrite rules use regular expressions. These regular expressions are always tiresome and difficult to maintain. The following, for example, is an answer to question about regex parsers on HTML, and the difficulty you may find with them:
RegEx match open tags except XHTML self-contained tags
Your htaccess file is an Apache configuration file. It should be used for simple configuration, and not for programming. Have it point to the code, and then let the code do the rest. For example, your .htaccess file:
RewriteRule ^(.*)$ index.php [QSA,NC,L]
And, if you're using PHP with index.php...
$objects = explode('/', ltrim($_SERVER[REDIRECT_URL], '/'));
print_r($objects); // list of items from URL
You may end up wanting to grab a different SERVER parameter, but you'll want to keep this complicated stuff in the code, not in configuration files.

Related

Redirect link to new link with anchor tag

I have the url: www.example.com/test/here.
I want to re-direct it to: www.example.com/test2/home#here.
I have tried:
RewriteRule /apply$ www.example.com/test2/home#here [R, NE]
But I am currently getting a server error. Sorry I am not very good with htaccess files.
You are getting a server error because of this:
[R, NE]
You can't have spaces in your rewrite flags, it confuses mod_rewrite and makes it think it's an extra parameter, thus making it think you've not closed your flags with a ].
Besides that, you're regex pattern: /apply$ will never match /test/here. Perhaps, you should try ^test/here$ instead.

Apache mod_rewrite path name as query parameters?

I want to use Apache's mod_rewrite in order to be able to take each folder of a path as a particular query parameter, for example consider the following:
Basic example
Url requested: http://domain.com/shoes/prada/image-1/
Page served: http://domain.com/?cid=shoes&bid=prada&pid=image-1
In this scenario, there are 3 sub-folders requested (/shoes/, /prada/ then image-1), so the first sub-folder is passed in the actual page served as cid, the second as bid and the third as pid.
Full example
However, I would also like it to serve a particular page depending on the number of sub-folders requested, e.g.
Url requested: http://domain.com/shoes/prada/
Page served: http://domain.com/shop.php?cid=shoes&bid=prada
So far all I've managed to find is regex based matching for mod_rewrite but my path's will vary a lot, which is why I would like to have conditions based on the number of folders accessed (please note, I'm not that good with regex - I reckon a wildcard character would help with this, but I wouldn't be sure where to start).
Any help on this would be greatly appreciated! This is pretty long winded, so if you need any more info for clarifying, please let me know!
With a little bit of work I was able to tweak some regex and get a working rule set for what I wanted:
RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/(.)?$ product.php?tid=$1&sid=$2&eid=$3 [L]
RewriteRule ^(.*)/(.*)/(.)?$ brand.php?tid=$1&sid=$2 [L]
RewriteRule ^(.*)/(.)?$ shop.php?tid=$1 [L]
This is a bit different to the example, however it's what I intended for in the first place.
This allows for the rewriting of url's up to four folders deep, with the "name" of each folder being given as a parameter, and each additional level of depth rewriting the url to a separate resource for example:
http://x.com/shoes/prada/2011-high-heels/ -> http://x.com/product.php?tid=shoes&sid=prada&eid=2011-high-heels
Tested on http://martinmelin.se/rewrite-rule-tester/

mod_rewrite to change query string parameter name

I need help writing a mod rewrite rule to change the name of a query string parameter. I want to change the name, not the value.
old name partner
new name a_aid
so a link like this
http://domain.com/?partner=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo
will become
http://domain.com/?a_aid=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo
I found this article but the accepted answer generated errors for the OP:
mod_rewrite - old parameter name to new name
also this article, but the solution was to use PHP. which will not work in my case:
APACHE mod_rewrite change variable name in query string
I can't use PHP because some affiliate tracking code creates a cookie from the query string--and expects the a_aid. So I'm trying to convert partner into a_aid for it
OK think I hacked it together on my own. Please post an answer if you think its brittle or could be done better and I'll accept yours instead
RewriteCond %{QUERY_STRING} ^(.*)partner(.*)$
RewriteRule ^(.*)$ $1?%1a_aid%2 [R=301,L]

How would I use mod_rewrite to turn this query string request in to a clean url?

How can I use mod_rewrite to take http://www.site.com/events?pg=4 and turn it to a clean URL, like so: http://www.site.com/events/4 ?
Thanks for the help!
Something like this should do the job:
RewriteEngine On
RewriteRule ^events/([^/]*)$ /events?pg=$1 [L]
What it's doing is:
Turning on the Rewrite engine
Matching any request that has the form events/something
Storing the 'something' (the brackets indicate that the match should be stored)
Redirecting to the ugly form using the stored variable.
Preventing any further rules from being applied ([L])
Hope this helps.

Abusing Mod Rewrite for a clean file system, and pretty URLs

I have a few php files that do a few different jobs. I'd like to change the way my clients access these php files to make it more clean for the end user. The Mod_Rewrite system has shown that it can do some pretty powerful things when in the hands of the right server admin. So I was wondering how far can you abuse the Mod Rewrite rules for a cleaner file system, and pretty URLs. Considering that the PHP files themselves use query strings to get their data, I'd like to alias the way the query string is built based upon how the how deep into the fake files system we go.
Our website's URL is http://www.domain.tld/, but we shall call it domain.tld for short. I'd like to map a few different address to a few different query strings on a few different files. But I'd also like to to be expandable on a whim.
Or first set would be, anything going past domain.tld/series/ should be directed to the domain.tld/series.php script with any (fake) directory past series to become part of the query-string for series.php. The same should happen to anything directed in the direction of domain.tld/users/ that should be redirected to the domain.tld/users.php file.
So if we had a URLs like, domain.tld/series/Master/2010/ or domain.tld/series/Novice/Season 01/ they would still be redirected to the domain.tld/series.php script, but with the query-string of ?0=Master&1=2010 and ?0=Novice&1=Season 01. But should I want to get an overview of the Master series, I could go the the URL domain.tld/series/Master/ and produce the query-string of just ?0=Master. The idea being that the rewrite rule should allow for infinite expandability.
This is how I'm doing it, and it sure works infinitely:
RewriteRule ^((/?[^/]+)+)/?$ ?q=$1 [L]
The trick is that the whole path is passed on as a single parameter, q, to index.php. So for example domain.tld/series/Novice/Season 01/ becomes domain.tld/?q=series/Novice/Season 01. Then you can do:
<?php
$params = explode('/', $_GET['q']);
var_dump($params);
?>
to get the individual parts.
array(3) { 0 => 'series', 1 => 'Novice', 2 => 'Season 01' }
It is not possible to be completely dynamic in such a system and have, as you say 'infinite expandability. You would have to define a RewriteRule for every 'tier' you will allow in your URL, or alternatively match everything after the first 'tier' as a single variable and do the work with PHP.
Example 1
RewriteRule ^([^/]+)/?$ /$1.php
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?0=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?0=$2&1=$3
Example 2
RewriteRule ^([^/]+)/(.*)/? /$1.php?qs=$2
Obviously these are only very simple examples and you'd probably have to use RewriteConds etc. to exempt certain files etc.