need .htaccess RewriteRule advise - .htaccess

I've looked around for an answer to this, no luck.
What i want to do is replace
http://localhost/mysite/superv/something with
http://localhost/mysite/superv/?p=something
Here's the best formula I came up with since yesterday:
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^(superv/)([^\?/]+)$ $1\?p=$2 [NC]
Yet it's not working.
I think the "RewriteBase" thingy has nothing to do with the problem because this line is working like a charm:
RewriteRule ^(javascripts/main\.js)$ $1\.php [NC]
Edit: Right Rekire, that was a mistake while copying and pasting the code. I've fixed the question now.
Edit2: Here's the error that appears in apache logs:
[Tue Mar 20 20:26:01 2012] [error] [client 127.0.0.1]
Request exceeded the limit of 10 internal redirects due to probable configuration error.
Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
I'm guessing the problem is "looping" and I quote:
Looping occurs when the target of a rewrite rule matches the pattern.
This results in an infinite loop of rewrites

This appears to be working for me.
RewriteRule ^(superv/)([^\?/]+)$ $1?p=$2 [R,NC]
Remove the R, if you want to do an internal redirect.

For some reason, this formula worked (internal redirect):
RewriteRule (.*)(superv/)?(.*) $1$2index.php?p=$3 [NC,QSA]
It seems to have broken the "infinite loop".

Related

.htaccess rewrite works but 503 redirect fails

Background
We, a group of whisky enthusiasts, only release a new whisky cask plan (product) once a week at Wednesdays at 1900 CET and since the demand to obtain a share in the casks exceeds the availability by many times, the server is more or less rampaged at 1900 CET and can thus do with all the server resources available.
Preferred Solution
After analyzing the logs of the last weeks I noticed that many of the > 800 members are also active simultaneously on the forum at the time of the release of the new cask plan (product). So to ease the load for the server a bit I want to make the forum temporarily unavailable between 1855 and 1915 CET, every Wednesday.
So I came up with the following code for the .htaccess to control this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/forum(/.*|$) [NC,OR]
RewriteCond %{REQUEST_URI} ^/timeline(/.*|$) [NC]
RewriteCond %{TIME_WDAY} =3
RewriteCond %{TIME_HOUR}%{TIME_MIN} >1654
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1715
RewriteRule ^(.*)$ /503-Wednesday-1900CET.php [R=503,L]
Some notes:
I also include the /timeline/ pages, just to be sure, as it uses a bit heavier query. So I want to exlude the possibility that the cache is refreshed during this window.
TIME_WDAY starts with Sunday at 0 so to just pass this condition on Wednesday =3.
The server is configured with the UTC timezone so I have to adjust for the timezone
offset which results in >1654 and <1715, instead of >1854 and <1915.
I can test the above, without the 3 RewriteCond %{TIME_ lines, successfully on https://htaccess.madewithlove.be/
But when I try it on my server, with the 3 RewriteCond %{TIME_ lines disabled, it results in a 503 message from the server but it doesn't actually rewrites the url to the /503-Wednesday-1900CET.php file...
When I try it on my server, with the 3 RewriteCond %{TIME_ lines enabled, it results in a 500 message from the server and also doesn't actually rewrites the url to the /503-Wednesday-1900CET.php file...
My Questions
What might be the reason that the /503-Wednesday-1900CET.php file isn't served? I suspect the RewriteRule contains something the server doesn't accept but I don't see it.
And what might be the reason that the 3 RewriteCond %{TIME_ lines result in a 500 error message instead?
I already searched the web and Stackoverflow but I don't seem to find the trick to get me forward with this. So any hint is welcome at the moment.
I can test the above, without the 3 RewriteCond %{TIME_ lines, successfully on https://htaccess.madewithlove.be/
The MWL tester tool is incorrect. When you specify a return status outside the 3xx range then the substitution string is ignored and the error document for the respective status code is served instead (or default server response if an ErrorDocument has not been explicitly defined). So, you need to define a custom ErrorDocument for the 503 status.
Not sure why you'd be getting a 500 error as there doesn't appear to be anything wrong with the directives you've posted, other than that mentioned above.
However, you do have to be careful that the error document itself does not trigger a 503 response - as this would result in an endless loop (500 error). You also have to be careful that any linked resources (images, CSS, etc.) are not also blocked. (Although you've not got so far as serving your error document yet).
Try the following instead:
RewriteEngine On
ErrorDocument 503 /503-Wednesday-1900CET.php
RewriteCond %{REQUEST_URI} ^/forum(/.*|$) [NC,OR]
RewriteCond %{REQUEST_URI} ^/timeline(/.*|$) [NC]
RewriteCond %{TIME_WDAY} =3
RewriteCond %{TIME_HOUR}%{TIME_MIN} >1654
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1715
RewriteRule !^503 - [R=503]
Note that the substitution string in the RewriteRule directive is just a single hyphen (-) - indicating no substitution (it's ignored anyway).
The !^503 pattern is to ensure that the error document itself does not result in a 503 response (effectively an endless loop, that would result in the server's default error response with an additional message along the lines of "a 500 error was encountered trying to serve an ErrorDocument to handle the request".) Likewise, you will need to make sure that any linked resources are not also caught by this rule. It is generally preferable to have the 503 response as basic as possible.
You don't need the L flag when specifying a status code outside of the 3xx range.
I removed the RewriteBase / directive since it's not required here. However, if you have other directives that rely on this then obviously add it back.
Strictly speaking a 503 response should also come with a Retry-After HTTP response header containing the time the service is expected to return. (I assume you are setting this in your PHP script.)

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.

How do you rewrite a URL using htaccess?

I'm attempting to use htaccess to write my urls as localhost/username instead of localhost/profile.php?id=username
I looked it up and this is what I found to be the solution
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(.*)$ /profile.php?id=$1 [L]
That's an exact copy from this stackoverflow question htaccess rewrite for query string however, for some reason it always returns a 500 error. I'm not sure why it's giving me this since I'm no expert with htaccess. I pretty much just find code modify it to fit my use.
Mod_rewrite is definitely turned on. So I know that can't be the issue. Any idea what is wrong with the code I have?
Edit:
This is what the apache error log returned.
[Sat Apr 27 14:20:10.558122 2013] [core:error] [pid 3244:tid 1688] [client 127.0.0.1:58653] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
If it's really a redirect loop per your comment, try adding this line immediately before your rule:
RewriteCond %{REQUEST_URI} !-f
The part where it says /profile.php?id=$1 is where it will rewrite you to. If that;s not a file then it would show up with an error so change that to whatever file you want it to rewrite you to.
Hope this helped
Kevin

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.

mod_rewrite rules logging "Config variable ${REQUEST_URI} is not defined" on every request

I have the following .htaccess on an Apache/2.4.2-win32 server:
# Turn mod_rewrite on
RewriteEngine On
# Allow direct loading of files in the static directory
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?static/(.+)$ - [L]
# Send all other requests to controller
RewriteCond ${REQUEST_URI} !^/?(spf/index\.php)?$
RewriteRule .* spf/index.php [L,QSA]
This works well and does exactly what I want it to. For those of you who can't be bothered working out what it does, it sends all requests through spf/index.php unless they are for a file that exists in the static directory.
The file resides in the virtual host's documentroot.
Every request that falls through this .htaccess generates the following error:
[Wed Aug 01 14:14:16.549835 2012] [core:warn] [pid 7100:tid 1076] AH00111: Config variable ${REQUEST_URI} is not defined
This is not actually causing a problem - every request works as expected - but it's filling up my error log and I don't like it.
According to Google, no-one has ever had this error before. That's as far as I've got with debugging it, I don't really know where to go next.
Anyone got any idea what's going on here?
P.S. I'm aware this might be a question better suited to SF, if the general opinion is that it doesn't belong here I'll move it.
You need to replace the $ with a %:
RewriteCond ${REQUEST_URI} !^/?(spf/index\.php)?$
to
RewriteCond %{REQUEST_URI} !^/?(spf/index\.php)?$

Resources