What is wrong with this rewrite rule? - .htaccess

Here's the rule in my .htaccess file:
RewriteRule ^add/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ add.php?link=$1&email=$2 [PT,L,QSA]
Here's the link that won't work (page not found):
http://www.example.com/add/http%3A%2F%2Fwww.example.com%2F/web%40web.com
Here's the version that will work (although not in the format I am hoping for):
http://www.example.com/add.php?link=http%3A%2F%2Fwww.example.com%2F&email=web%40web.com
How do I fix this rewrite rule?

Your regex i.e. [A-Za-z0-9-] not match %3A, %2F etc, try this:
Options -MultiViews
RewriteEngine On
RewriteRule ^add/(.+)/([^/]+)/?$ add.php?link=$1&email=$2 [NC,L,QSA]
Also %2F is not allowed in Apache by default. Use
AllowEncodedSlashes Decode
in your Apache config to allow it otherwise Apache will throw 404 if there are any %2F in URL even before mod_rewrite is invoked.

Ok, I have solved this problem and it took a great deal of research as I'm not as talented as some people on here. I thought I'd share the solution anyway.
Essentially the server will prematurely decode %2F as soon as it is used, so you end up with a path that is totally wrong.
What you have to do is replace %2F with %252Fat the client side.
This is the double encoded form of %2F.
So when it reaches the server it prematurely gets decoded to %2F instead of a forward slash.
You're welcome.

Related

I am not sure if my .htaccess is wrong or if the server didn't read my .htaccess file yet

I am pretty certain, that my code is alright, for a briefly moment, my htaccess seems to work when i entered an url. After I extended the file and reuploaded it, my Rule didn't work any more. Even when I removed my edit the Rewrite Rule don't triggert any more.
Did i miss something in my Rule. I don't get it whats wrong right now.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^www\.sub\.domain\.de\/(.*)\.html$ www.sub.domain.de/typo3/$1.html?&%{QUERY_STRING}
</IfModule>
Okay. My first mistake was the complete url as first param in RewriteRule.
After I followed Kaddath's instuctions the rewrite kinda worked. I still had a problem with a loop call. The htaccess appendes planty "typo3/typo3/...", so that a RewriteCondition has to solve this. I checked if the url has already typo3 in his string. Now the Url is correctly rewritten.
I also tried [L] before to stop a looping rewriteRule, but it didn't help.
Thank you Kaddath for giving the right clues.

Set an index page for a specific folder in .htaccess

What I want to do should be quite simple: when the admin writes www.example.com/admin I want that he's addressed to www.example.com/admin/admin_index.php.
I wrote this rule and I checked on other posts here on Stackoverflow: it apparently seems to be correct, but it actually doesn't work.
RewriteEngine On
RewriteRule ^/admin/?$ /admin/admin_index.php [L,NC]
Has anyone any clue on why the redirect doesn't work, since it "stays" at www.example.com/admin outputting (obviously) the 403 error?
There is no need to use a rewrite rule. Just use DirectoryIndex directive in admin/.htaccess:
DirectoryIndex admin_index.php
This will load admin/admin_index.php when a request comes for http://domain.com/admin/

Redirects not working as expected

I have an .htaccess file with several lines. It does not work as expected. Mod_rewrite is enabled. RewriteLogLevel is set to 9.
The first two rules are there to forbid uris with a length more then 80 characters:
RewriteCond %{REQUEST_URI} ^.{80}
RewriteRule .* - [F]
It does not seem to get evaluated as every test url passes through and it does not generate an error either.
I also tried:
RewriteRule .{80} - [F]
But that did not do the trick either. The process ends with a 404, not a 403.
This next rule is not working either. It used to work.
RewriteRule ^(\/)?([\w]+)$ /index.php [L]
The URI /Contact was always handled by this index.php.
Whatever URL I type I get a 404. I should get a 403 or a 200. Not a 404. What am I missing?
Apache has on all directories the permission to read, write and execute and on all files the permission to read and write.
The two urls for testing are:
127.0.0.4/asssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssddddddddddddddddddddd?p=s&s=psv
and
127.0.0.4/Contact
The alias for 127.0.0.4 used is considerate.lb.
Try this rule instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\S{80}
RewriteRule ^ - [F]
Using THE_REQUEST instead of REQUEST_URI as that variable might get overwritten due to presence of other rules in your .htaccess
Finally I have found a solution. The problem was not in the coding of the .htaccess. I replaced the file with a previous version, added the new lines to test the request and it worked all fine.
It is not a satisfactory solution, because it can happen again and I do not have any clue what caused the error. If someone knows the error, I would love to hear what might have been the exact cause and how to solve that properly. I would like to change the tags of the question as the current tags might be misleading (although other people might experience the same problem how apache handles a .htaccess file), but I do not know which tags I should use.

using mod_rewrite to strip out junk

We're seeing some really weird URLs in our logs and I've been told to start redirecting them.
I know of a couple of better ways to go about fixing this, but the boss wants it done this way. I apologize in advance.
We're seeing stuff like the following in our logs:
http://www.example.com/foo/bar/bla&ob=&ppg=&rpp=100&ob=&rpp=&ppg=&rpp=30&ppg=&ppg=1&rpp=10&rpp=50&ob=&ob=&ob=&rpp=40&ob=&rpp=5&rpp=30&rpp=&rpp=20&order_by=&results_per_pge=75
I've been told to 'toss some mod_rewrite rules in the .htaccess file' to take this and strip out all the ob, rpp, and ppg variables.
Now, I've found ways to strip everything out. And that wouldn't be too bad if I could leave the /foo/bar/bla in there. But I can't seem to do that. Basically, any help would be appreciated.
Try:
# strip out any params that's ob=, rpp= or ppg=
RewriteRule ^/?(.*)&ob=([^&]*)&(.*)$ /$1&$3 [L]
RewriteRule ^/?(.*)&rpp=([^&]*)&(.*)$ /$1&$3 [L]
RewriteRule ^/?(.*)&ppg=([^&]*)&(.*)$ /$1&$3 [L]
# if everything's gone, finally redirect and fix query string
RewriteCond %{REQUEST_URI} !&(ob|rpp|ppg)
RewriteRule ^/?(.*?)&(.*) /$1?$2 [L,R=301]
The problem here is that your URL:
http://www.example.com/foo/bar/bla&ob=&ppg=&rpp=100&ob=&rpp=&ppg=&rpp=30&ppg=&ppg=1&rpp=10&rpp=50&ob=&ob=&ob=&rpp=40&ob=&rpp=5&rpp=30&rpp=&rpp=20&order_by=&results_per_pge=75
has A LOT of ob=, rpp=, and ppg= in the URI. More than 10. That means you'll get a 500 internal server error if you use these rules against that URL. By default, apache has the internal recursion limit set to 10, that means if it needs to loop more than 10 times (and it will for the above URL), it'll bail and return a 500. You need to set that higher:
LimitInternalRecursion 30
or some other sane number. Unfortunately, you can't use that directive in an htaccess file, you'll need to go into server or vhost config and set it.

Problem with htaccess redirect with rewrite module

I have an old url at:
http://example.com/search/admin
I want to make it go to:
http://example.com/cgi-bin/admin
This is what I have so far, which could be completely wrong...
RewriteRule ^/search/admin$ https://example.com/cgi-bin/admin
The mod_rewrite is definitely on and working and I am using apache 2.2.
Little correction to Benubird post:
RewriteRule ^search\/admin\/?$ cgi-bin/admin [L]
RewriteRules never start with a slash and the redirect can be without slash.
the ^ character matches the start of a line ( or url in this case). Just a thought, but maybe your line should be:
RewriteRule ^/search/admin$ /cgi-bin/admin
Otherwise I suspect you'll either not be redirected, or be getting redirected to https://example.comhttps://example.com/cgi-bin/admin

Resources