Rewrite with both slash and no slash - apache

I have a bit of a tricky situation that I'm not sure how to go about finishing.
I have the following code which redirects mysite.com/i/somelink/ to mysite.com/view.php?i=somelink
RewriteRule ^/?i/([^/]+)/$ /view.php?i=$1 [L]
The issue is, the code above only works when there is a trailing slash at the end of mysite.com/i/somelink/
I need it to work both when there is, and is not a slash.
The tricky part is that I'm also trying not to use a separate rule to reduce stress on the system, and the rule needs to be written in such a way that it does not interfere with mysite.com/i/somelink.ext, which also exists, and will be embedded in the file (view.php) I'm redirecting too.
I'm probably making it much more complicated than it needs to be, but basically I just need to make the rule above work both with and without trailing slashes.
Any tips are appreciated. Thanks.

Change it to:
RewriteRule ^/?i/([^/]+)/?$ /view.php?i=$1 [L]

Related

Apache Rewrite Rule gives 404 Paypal IPN Redirection

I have been trying to get a rewrite rule working to redirect my old paypal IPN URL to my new one, I have tried the following in the .htaccess file and cant seem to get it to work, any help would be greatly appreciated!!
# BEGIN PayPal Fix
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/wc-api/WC_Gateway_Paypal/
^/hosting/modules/gateways/callback/paypal.php [R=307,L]
</IfModule>
END PayPal Fix
Another thread on stackoverflow about a similar issue: Change the IPN url on existing subscription
See jon G's post about his rewrite, that is exactly what im trying to accomplish!
Thank you for any and all guidance!
There seem to be some problems with your rewrite rule.
The minor problem is that it is unusual to write such a rule in two lines. In fact, I am not sure if this is allowed in the form you use; I would expect that there would have to be at least a line continuation character. So I would put the rule into a single line first.
But the main problem is in the target of your rewrite rule. While the first part of a rewrite rule is a search pattern (regular expression), the second part (target) isn't, so you should leave away the leading ^.
Your rule then should look like this:
RewriteRule ^/wc-api/WC_Gateway_Paypal/ /hosting/modules/gateways/callback/paypal.php [R=307,L]
But I am wondering why there is no script name in your search pattern. Do you really want to map a directory to a script? Or does WC_Gateway_Paypal just appear as directory, but in fact already is mapped to a script by a rewrite rule which is evaluated before?

mod_rewrite on parent and subfolder

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/([^/]+)/?$

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!