.htaccess RewriteRule - multiple replacements needed - apache

I'm a photographer, and run a website with thousands of event photos.
Over the years the path to many directories has changed, and, with a new software, the access to individual photos has changed as well.
I get the path changes covered like this (there are many more rules):
RewriteRule ^([0-9]{4})-major-generals-review/?(.*)$ /galleries/trooping-the-colour/$1-major-generals-review/$2 [R=301,NC,L]
RewriteRule ^([0-9]{4})-bcn-challenge/?(.*)$ /galleries/canal-and-waterway-events/$1-bcn-challenge/$2 [R=301,NC,L]
But I also have to replace, on top of the above, access to individual photos.
The "old" form is, to use a concrete example,
2013-bcn-challenge/index.php?`page=11/thumbnails/1305241839185D24543HaraldJoergens_v1.jpg&autoload=1305251137045D25451HaraldJoergens_v1`
and what I need is
galleries/canal-and-waterway-events/2013-bcn-challenge/1305241839185D24543HaraldJoergens_v1-single.php
so there are two replacements needed,
2013-bcn-challenge with /galleries/canal-and-waterway-events/2013-bcn-challenge/
(that part does work), and
index.php?page=11/thumbnails/1305241839185D24543HaraldJoergens_v1.jpg&autoload=1305251137045D25451HaraldJoergens_v1 with
1305241839185D24543HaraldJoergens_v1-single.php
which does not work. My code for the second replacement is
RewriteRule (.*)/index.php?page=[0-9]+/thumbnails/([-_0-9a-zA-Z]+)\.jpg.* $1/$2-single.php [R=301,NC]
on top of the other rules. Unfortunately, it seems to result in an endless loop.
To make matters worse, instead of "/thumbnails/", it could be "/cust_thumbnails/" or "/photos/", and the "autoload" part can be there or not.
Might anyone be able to help me get around the problem? Thanks a lot in advance!
Harald

The problem is that the query string (?page=...) is not part of the request URI, and so cannot be matched in the rewrite pattern.
As such, you will need to check the query string separately:
RewriteCond %{QUERY_STRING} ^page=\d+/((cust_)?thumbnails|photos)/([\w\-]+).jpg [NC]
RewriteRule ^(.*/)?index.php$ /$1%3-single.php? [R=301,NC,L]
Here, we are checking the query string for the presence of page=<num>, cust_thumbnails, thumbnails, photos, and, of course, the name of the photo.
Then, we are redirecting to /<folder-name>/<file-name>-single.php.
Note: In the expression, I have changed [0-9]+ to \d+ and [-_0-9a-zA-Z]+ to [\w\-]+ - these are both shorthand alternatives.

Related

redirecting multiple mod-rewrite rules to one

I don't know how to title the question, but here's my problem.
I'm trying to turn multiple rules into one rule pointing to one page.
The website has a catalog of photos which can be accessed preferably with the following url structure and the image numbers must start with zero:
(insert base url)/photocategoryname/month-01-2014/image/1
I want (insert base url)/photocategoryname/month-01-2014/image/0 to automatically switch to and load data from (insert base url)/photocategoryname/month-01-2014/image/1
I also want (insert base url)/photocategoryname/month-01-2014/image-1 to convert to (insert base url)/photocategoryname/month-01-2014/image/1 as well because I did a URL redesign and in the past I used image-1 instead of image/1, and search engines are trying to retrieve old files.
I also want to handle cases where an extra slash is added by accident.
When I used this ruleset and tried accessing (insert base url)/photocategoryname/month-01-2014/image-1, it gave me a redirect page with the new URL as (insert base url)/photocategoryname/month-01-2014/image/1/month-01-2014/image-1.
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/image-([0-9]+)(/)?$ /$1/$2-$3-$4/image/$5 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/([0-9]+)(/)?$ /$1/$2-$3-$4/image/$5 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/([0-9]+)/(.+)$ /$1/$2-$3-$4/image/$5 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/image/0(/)?$ /$1/$2-$3-$4/image/1 [NC]
RewriteRule ^(.+)/(.+)-([0-9]+)-([0-9]+)/image/([0-9]+)$ /photo.php?TITLE=$1&DATE=$2-$3-$4&PHOTONUMBER=$5 [NC,L]
What I want to do is minimize redirects to maybe 1. I was able to get this setup working before when I used [R=301,NC,L] instead of [NC] for the first 4 rules, but that caused too many redirects and I don't want my server spending resources on handling unnecessary redirects.
Any idea what I could be doing wrong?
UPDATE:
Here's what I'm trying to achieve.
I want to allow people to access the same page (photo) using any one of the following URLs:
(insert base url)/photocategoryname/month-##-####/image#
(insert base url)/photocategoryname/month-##-####/image/#
(insert base url)/photocategoryname/month-##-####/image-#
(insert base url)/photocategoryname/month-##-####/photo#
(insert base url)/photocategoryname/month-##-####/photo/#
(insert base url)/photocategoryname/month-##-####/photo-#
(insert base url)/photocategoryname/month-##-####/picture#
(insert base url)/photocategoryname/month-##-####/picture/#
(insert base url)/photocategoryname/month-##-####/picture-#
and I made PHP code that can help redirect the user to the proper URL:
(insert base url)/photocategoryname/month-##-####/image/#
This old code as the second parameter for the rewrite rule works but only for the one URL.
^(.+)/(.+)-([0-9]+)-([0-9]+)/image/([0-9]+)$
But this one seems to be ignored entirely on the server:
^([^/]+)/([A-Z0-9]+)-([0-9]+)-([0-9]+)/(image|photo|picture|photograph|pic|)(-|/|)([0-9]+)/*$
Both lines of code end with [NC,L].
What could I be doing wrong?
The way you did it before was correct, the first 4 lines should have a R=301 for a proper redirect.
It's not good practice, but if you want make the same page available under these different URLs, you could try changing [NC] to [NC,L]

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/

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.

How to write rewrite rule in htaccess file

I have a problem with rewrite rule
my link
is www.something/group/group_id/place/groupName
for this
rewriteBase /
RewriteRule ^group/(.*)/(.*)/(.*)$ /group.php?gid=$1 [QSA,NC,L]
somet times my url may come www.something/group/group_id/groupName.
In Both cases I have to rewrite to group.php and I need only groupid. How to write rewrite rule to work in both situation?
Either use lazy quantifiers or prevent each matching group from matching the / itself. The way you have it currently, the first group will match as much as it can resulting in unwanted results.
RewriteRule ^group/(.*?)/(.*?)/(.*?)$ /group.php?gid=$1 [QSA,NC,L]
RewriteRule ^group/([^\/]*)/([^\/]*)/([^\/]*)$ /group.php?gid=$1 [QSA,NC,L]
An even better way, to allow people to leave out unnecessary parts (read: not needed to evaluate the result on the server side), you could even do something like this:
RewriteRule ^group/(\d+)(/.*)?$ /group.php?gid=$1 [QSA,NC,L]
(This is based on the assumption that your group id is a number)
Try this one:
^group/(.+)(/|/.+)*$
It matches for
www.something/group/group_id/place/groupName
www.something/group/group_id/groupName
I never used the RewriteRule, so it's not tested. And maybe if you add the "Regex" Tag to your question you'll get more answers ;-)

rewrite urls with GET reqs

how do I configure my .htaccess rewrite rules to accomodate GET requests?
Currently, /manager/page goes to: ?dept=manager&n=page however, some pages have additional GET reqs, and so this rule doesn't work:
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&n=$2 [QSA]
I would need: ?dept=manager&n=page&id&etc=etc to go to: /manager/page/id/5/etc/6 however, not all pages present the same method of id input, IE. some pages used id, others catid, and others, bugid, so it's a bit difficult.
Thanks :)
UPDATED: END URL - id/5/etc/6
You just need to sepcify a different rule.
RewriteCond ${HTTP_METHOD} "GET"
RewriteRule --your rule--
If your input methods are really that varied, you should use multiple RewriteRules for each different format.