mod_rewrite on parent and subfolder - apache

I ran into some strange behaviour using mod_rewrite under Apache. Here's how the current RewriteRules look:
RewriteRule ^(.*)/collections/(.*)/(.*)/?$ bootstrap.php?controller=category&user=$1&collection=$2&category=$3 [L]
RewriteRule ^(.*)/collections/(.*)/?$ bootstrap.php?controller=collection&user=$1&collection=$2 [L]
My expectations of the above rules is that accessing a URL such as domain.com/BenM/collections/0/1/ should take me to the category controller, while domain.com/BenM/collections/0/ should take me to the collection controller.
At the moment, both URL structures rewrite to bootstrap.php?controller=category....
My understanding was that if the [L] flag is specified, Apache looks no further and performs the rewrite.
Could anyone point in the right direction here, as I just cannot beat this... What should the rewrites look like to achieve the functionality I explained above?

You're dealing with greediness-related issues. Also, you probably want a length of at least one, and you almost certainly don't want slashes in your params.
Potential solution:
^([^/]+)/collections/([^/]+)/([^/]+)/?$
^([^/]+)/collections/([^/]+)/?$

Related

mapping RewriteRule to RewriteMap

we're doing a migration and I need to redirect several hundred thousand old urls to new locations. I can get a proof of concept working with mod_rewrite but I know this will not scale. I've been reading about RewriteMap and I think it will do what I need but I can't get it to work.
the search strings can appear anywhere in the url making things more complicated.
the rewrite rules that work look like this
RewriteRule "876f7103-73fd-470b-a5bc-584bcd5135da" "https://XXXXX/V/Redirect?oldDirectLink=https://YYYY/Watch/y4L9TeKs"
RewriteRule "y4L9TeKs" "https://XXXXX/V/Redirect?oldDirectLink=https://YYYY/Watch/y4L9TeKs"
I can't figure out how to convert this to rewritemap... any help would be appreciated...

Mass re-write rule that keeps the existing URL structure and only rewrites the end of the URL?

I'm currently working on a plumbing website which has both multi lingual and multi regional content, below are some examples of the current URL structure...
http://www.domain.com/product/de/?region=uk
http://www.domain.com/product/plumbing-and-heating/de/?region=uk
http://www.domain.com/product/plumbing-and-heating/de/?region=uk
For SEO purposes i'm looking to rewrite the end of these URLs to be...
http://www.domain.com/product/de-uk
http://www.domain.com/product/plumbing-and-heating/de-uk
http://www.domain.com/product/plumbing-and-heating/de-uk
I would also settle for...
http://www.domain.com/product/de/uk
http://www.domain.com/product/plumbing-and-heating/de/uk
http://www.domain.com/product/plumbing-and-heating/de/uk
I know I could do something like...
RewriteCond %{QUERY_STRING} (^|&)region=uk($|&)
RewriteRule ^product/de/$ /product/de-uk/? [L,R=301]
which would rewrite...
http://www.domain.com/product/de/?region=uk to be http://www.domain.com/product/de-uk/
However as there are obviously a lot of different languages, regions and subdirectories to do these one by one would be a massive task as their are many different combinations!
Therefore i'm looking for an easier more manageable solution, any help appreciated?
Something like this might help:
RewriteRule ^([^/]+)/([^/]+)/([^/-]+)-([^/-]+)$ /$1/$2/$3/?region=$4 [NC,L]
RewriteRule ^([^/]+)/([^/-]+)-([^/-]+)$ /$1/$2/?region=$3 [NC,L]

simple mod_rewrite q...is the solution simple too?

I've looked at many examples here and all over the internet, but I can't seem to find an answer I understand, or that accurately solves my problem. I'm looking to implement a mod_rewrite directive in an .htaccess file that renames a folder to another name but does not show the name in the url bar.
For example (the user clicks a link that directs them to):
theSite.com/folder1/folder2/folder3/
I want them to see (same as above)
theSite.com/folder1/folder2/folder3/
But I want the browser to silently function in this directory
theSite.com/folder1/some_other_folder/folder3/
I am a PHP developer, writing my first web application. I can configure apache, PHP, mysql and use them like a pro. I'm sorry, but I don't understand the syntax for mod_rewrite. I can't seem to grasp it despite looking at many tutorials as I would need to ask questions before I could move onto the next concept. Thank you for your patience.
Your case is pretty run-of-the-mill. You just need to match the static string, plus a (.*) to match everything that follows it and store it into $1, then substitue some_other_folder.
The [L] flag (and absence of the [R] flag) instructs Apache to rewrite internally without redirecting the browser, and to stop here without matching further rules.
RewriteEngine On
RewriteRule ^folder1/folder2/folder3(.*)$ folder1/some_other_folder/folder3$1 [L]
If folder3 itself is part of the "dynamic" portion, that is, anything after folder2 should be silently rewritten into some_other_folder, leave folder3 out of the rule and just capture everything that follows folder2 into $1.
RewriteEngine On
RewriteRule ^folder1/folder2/(.*)$ folder1/some_other_folder/$1 [L]
I would use following
RewriteRule /folder1/folder2/folder3/ /folder1/some_other_folder/folder3/ [L]

mod_rewrite, trailing slashes - one line instead of two?

I have been playing with mod_rewrite using .htaccess to translate some directories - for purposes of both improved SEO and also to produce friendlier/more memorable URLs.
The only problem I can't crack at the moment is with trailing slashes. The behavoir I want is that you should be able to access the link with or without a trailing slash, just to cut down on missed traffic.
My real url is as:
http://www.mydomain.com/shipyard/index.php
I would like people to be able to access it via:
http://www.mydomain.com/shipyard/
http://www.mydomain.com/shipyard
http://www.mydomain.com/ships/
http://www.mydomain.com/ships
Of course, the top two are covered because thats actually a real and accessible URL, but I plan to tell Google that the best way to get at the page is using /ships (without having to move directories, break existing links etc).
The best I came up with so far was:
RewriteRule ^ships/$ /shipyard/index.php [L]
RewriteRule ^ships$ /shipyard/index.php [L]
However I just KNOW that i'm using two lines where only one is needed, but whatever I tried, I couldn't get the one! I know i'm missing something incredibly basic and/or obvious, but I need a pointer... Thanks!
RewriteRule ^ships(/)?$ /shipyard/index.php [L]
This means that the slash may or may not be present.

Creating rewrite rules for multiple urls in the same folder

I have been asked by our client to convert a site we created into SEO friendly url format. I've managed to crack a small way into this, but have hit a problem with having the same urls in the same folder.
I am trying to rewrite the following urls,
/review/index.php?cid=intercasino
/review/submit.php?cid=intercasino
/review/index.php?cid=intercasino&page=2#reviews
I would like to get them to,
/review/intercasino
/submit-review/intercasino
/review/intercasino/2#reviews
I've almost got it working using the following rule,
RewriteRule (submit-review)/(.*)$ review/submit.php?cid=$2 [L]
RewriteRule (^review)/(.*) review/index.php?cid=$2
The problem, you may already see, is that /submit-review rewrites to /review, which in turn gets rewritten to index.php, thus my review submission page is lost in place of my index page. I figured that putting [L] would prevent the second rule being called, but it seems that it rewrites both urls in two seperate passes. I've also tried [QSE], and [S=1]
I would rather not have to move my files into different folders to get the rewriting to work, as that just seems too much like bad practise. If anyone could give me some pointers on how to differentiate between these similar urls that would be great!
Thanks
(Ref: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
What I would do, is make /submit-review/ post directly to itself (or a php file) then once submitted redirect from within the PHP file.
It can be hard to force htaccess to maintain post values whilst redirecting etc
My friend found a solution to this one.
RewriteRule review/submit.php - [L]
Will catch the first rewrite and then prevent the next one, worked a treat!