Creating rewrite rules for multiple urls in the same folder - apache

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!

Related

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]

Apache: How to rewrite some dynamic URLs

I need to rewrite some specific URL's on my website but cannot find out how to do it despite searching for quite some time.
The original url that needs to be matched is in this format:
http://www.example.com/gallery/?level=picture&id=49
and I need them to be in this format:
http://www.example.com/index.php?page=gallery&level=picture&id=49
However, the folder also contains some image files as well. I need any URL's pointing directly to images to be left alone, and any URL's pointing to a php page to be rewritten as above.
I know what I want to do, but not how to do it. Basically I need to do this in .htaccess:
if(url contains 'gallery/' AND filetype != bmp/jpg/png/etc){
REPLACE '/gallery/' WITH '/index.php?page=gallery&' AND append original query string variables
}
Any help at all would be greatly appreciated.
So I have found a solution that has taken care of this problem for me. I have this working in my .htaccess file now.
RewriteCond %{REQUEST_URI} !\.(bmp|jpg|jpeg|png|gif)$
RewriteRule ^gallery/$ /index.php?page=gallery [R=302,QSA]
The first line (from what I have been told) excludes the file types listed from being affected by this rule, because as I originally mentioned I did not want the URL rewritten for images.
The second line takes a url like this:
http://www.yoursite.com/gallery/?level=picture&id=52
and turns it in to this:
http://www.yoursite.com/index.php?page=gallery&level=picture&id=52
and it leaves the original query string in place, in addition to the new "page=gallery" variable. It also does a 302 redirect so that the user is shown the correct address in their browser.
Not sure if this is helpful to anyone, but figured that since I posted asking about it, that I would post the solution I found as well.
This may a bit more general than you want, but it'll also avoid rewriting other existing files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^/gallery/(.+)$ index.php [QSA]

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.

We've created a new website, but the old URLs should continue to work because people have bookmarked them

The only problem is
The old urls are something like this www.example.com/?pt#!/2/1270/something-etc-etc/
and we want to redirect them, but we need to pass the something-etc-etc to the new url.
Something like this new.example.com/old/ plus(something-etc-etc)
I've been trying so many ways that I'm already lost
RewriteCond %{QUERY_STRING} ([:alnum:]-)+?[:alnum:]/$
RedirectMatch www.example.com/ http://new.example.com/old/
I was hoping that this regex will return only the ending part, but instead, it returns ?pt#!/2/1270/something-etc-etc/
Best way is to use the Apache mod_rewrite module as described in the URL Rewriting Guide. A bit of a heavy read if you haven't used mod_rewrite before, but well well worth learning. Lots of examples to make things concrete, too.

Redirecting a Directory to a Script on Apache

So I'm playing with a script that makes it super easy to mirror images off of the web. The script works great (based off of the old imgred.com source, if you've seen that) problem is, it looks a little clunky when using it.
Currently, in order to use the script, you go to a url like:
http://mydomain.com/mirror/imgred.php?Image=http://otherdomain.com/image.jpg
What I'd like to do is to be able to go to:
http://mydomain.com/mirror/http://otherdomain.com/image.jpg
and have it redirect to the former URL, preferably transparent to the user.
I'm reasonably certain that this can be done via .htaccess with a MOD_REWRITE of some kind, but I'm getting frustrated trying to get that to work.
After messing with this myself, I found out that apache collapses any double slash in the URL before the query part into a single slash, and passes the result to mod_rewrite. Maybe that was giving you problems?
This might work for you (.htaccess in the mirror directory):
RewriteEngine On
RewriteBase /mirror
RewriteRule ^http(s?):/(.*) imgred.php?Image=http$1://$2 [L]
Don't know if your script accepts https addresses as well, so I included that just to be sure