ERR_TOO_MANY_REDIRECTS htaccess - .htaccess

Could anyone can explain me what this .htaccess code doing?
RewriteRule ([0-9]+)/([^t]{1}[A-Za-z0-9_-]{0,})\.([^s]{1}[A-Za-z0-9]+)$ ../items/go.php?u=$1/$2.$3
I have a folder : mywebsite.com/download/folder1/ with a .htaccess file in the uplevel mywebsite.com/download/ folder contains code above.
When I access a file directly to the path mywebsite.com/download/folder1/file.mp4 it returns error :
mywebsite.com redirected you too many times. Try clearing your
cookies. ERR_TOO_MANY_REDIRECTS
I tried to clear my browser cookies but the issue persist.
This issue is solved when I commented out the htaccess line above.

This rule appears to be looking for a specific pattern consisting of:
<numbers>/<alphanumeric string not starting with t and with other conditions applied>.<alphanumeric string not starting with s>
then redirecting the user's browser to go.php with the parsed filename passed as file arguments.
ERR_TOO_MANY_REDIRECTS is likely due to an infinite redirect loop. Without knowing what other RewriteRule statements are present it is difficult to narrow down an exact cause. You can narrow it down further on your end by seeing if the rewritten output might match any other RewriteRule conditions.
In general, the rule looks suspicious for the example input given. If this is home-grown I would suggest rewriting it entirely, as its current form will given unexpected outputs for user-generated data that could cause any security holes present in the go.php script to be easily exploited / exposed.

Related

Rewrite Url Htaccess for profile user with "." (dot) htaccess not working

I need to implement a rewrite rule for my users, this type:
https://domain/user.strike18
The problem is that using this regex, any url containing "dot" returns error.
For example
https://domain/register.php
https://domain/styles.css
So I added a dot inside the regex rule.
RewriteRule ^([#a-zA-Z0-9._-]+)$ user.php?p=profile&username=$1
RewriteRule ^([#a-zA-Z0-9._-]+)/$ user.php?p=profile&username=$1
Still not working, How do I make it work?
The issue you face is that it is impossible for the rewriting module to decide whether something like "abcde.fghi" is a user name or a file name with a "file name extension".
You could try to work around that issue by explaining the exceptions to the rewriting module, here by means of a condition that allows the application of the actual rule to only get applid of the requested path does not point to an existing file:
RewriteEngine on
RewriteCond {REQUEST_FILENAME} !-f
RewriteRule ^([#a-zA-Z0-9._-]+)/?$ user.php?p=profile&username=$1
That however has the annoying implication that this now won't work as expected for a user called for example "styles.css" ...
The issue is actually rooted not in your rewriting attempts but in how you try to publish those user profiles. You should use a unique prefix for that to prevent such unsolvable problems. So a URL like for example https://example.com/profile/user.strike18. That would obviously allow for a precise rewriting rule without any such naming collisions you currently face.

Renaming and redirecting pages fails in htaccess

I am sorry to ask this question, because the answer seemingly is so easy. However, after three hours of trial and error I am without a clue.
I have several pages on a website using parameters in the url. I would like to change that, to a more regular url. Example:
domain.com/pag.php?id=1-awesome-page should become domain.com/awesome-page
So far so good, but so far I have three problems.
1. The old page still is accessible, Google will index it as duplicated content. When I try to redirect it, I am getting infinite loop errors.
2. For whatever reason, sometimes SOME images (straight from the content) get stripped off on the newly named page. I tried playing with a base-url and renaming the images and urls, but nothing so far.
3. Also the redirect doesn't care if i'd enter id=1-awesome-page or id=2-worthless-page. It all redirects to the first one.
Among the things i've tried.
RewriteCond %{QUERY_STRING} id=1-awesome-page
RewriteRule ^pag\.php$ /awesome-page? [L,R=301]
RewriteRule ^awesome-page?$ pag\.php?id=1 [NC]
What you want to do cannot really be done with mod_rewrite, unless you want to make a rule for every page, which will probably slow your site down quite a lot. This is, because you can't summon the 1 in 1-awesome-page out of thin air, and your pag.php page doesn't seem to be able to load the page only based on it's seo name. If you need to use that number, you need to have that number somewhere in your url.
As for your questions:
The error you mention cannot be reproduced with the current iteration of your .htaccess. You likely had an infinite loop previously, and since you use R=301 to test, the browser will cache this redirect and only request the second resource afterwards when you request the first resource. You should test with [R,L] and only change to [R=301,L] when everything works as expected. Not doing so will cause weird behaviour, and behaviour you do not expect with your .htaccess.
When you have an url a and an url b, and want to redirect a to b, and want to internally rewrite b to a, you need to make sure that any given time not both rules can be matched. You can either use the %{THE_REQUEST} trick or use the END flag. Both are outlined in this answer.
If you have a problem with resources on a page not loading after making a fancy url, you likely used relative url's. This question outlines the possibilities on how to resolve this. You can either make the url's absolute or relative to the root of your site, or use <base href="/">.
The following would work for /pag.php?id=123-news-page and /news/123/news-page.
RewriteCond %{THE_REQUEST} pag\.php\?.*id=([^-]+)-([^&\s]+)
RewriteRule ^pag\.php$ /news/%1/%2? [L,R]
RewriteRule ^news/([^/]+)/([^/]+)/?$ pag.php?id=$1-$2 [L]

Correct extra spaces in URL with .htaccess

Can .htaccess be used to correct extra space errors in URL encoded links?
For example:
Incorrect: http://%20www.google.com/
Correct : http://www.google.com/
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\%20$ $1 [NC]
Something to that effect should work, don't count me on it.
Regex will help you.
http://corz.org/serv/tricks/htaccess2.php
If you have captured all your error pages, you could write something in the server language of your error page e.g. error.php and get that to do the string edit for you rather than rely on htaccess, as you may not want to really replace all spaces with nothing as files can have spaces in their name. String comparisons are expensive so may be best to only do it if there is an error, depends on your needs.
I have concluded that removing the leading space is not possible though .htaccess or error pages. Trying to access that page will return a Error 105 (Unable to resolve the server's DNS address). Therefore, .htaccess or a server error page will have no effect as the client cannot reach the server.

mod_rewrite so a specific directory is removed/hidden from the URL, but still displays the content

I'd like to create a rewrite in .htaccess for my site so that when a user asks for URL A, the content comes from URL B, but the user still sees the URL as being URL A.
So, for example, let's say I have content at mydomain.com/projects/project-example. I want users to be able to ask for mydomain.com/project-example, still see that URL in their address bar, but the browser should display the content from mydomain.com/projects/project-example.
I've looked through several .htaccess rewrite tips and FAQs, but unfortunately none of them seemed to present a solution for exactly what I've described above. Not everything on my domain will be coming from the /projects/ directory, so I'd imagine the rewrite should check to see if the page exists first so it's not appending /projects/ to every url. I'm really stumped.
If a rewrite is not exactly what I need, or if there is a simple solution for this problem, I'd love to hear it.
This tutorial should have everything that you need, including addressing exactly what you are asking: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html . It may just be a matter of terminology.
So, for example, let's say I have content at mydomain.com/projects/project-example. I want users to be able to ask for mydomain.com/project-example, still see that URL in their address bar, but the browser should display the content from mydomain.com/projects/project-example.
With something like:
RewriteRule ^project-example$ /projects/project-example [L]
When someone requests http://mydomain.com/project-example and the URI /project-example gets rewritten internally to /projects/project-example. Note that when this is in an .htaccess file, the URI /project-example gets the leading slash removed when matching.
If you have a directory of stuff, you can use regular expressions and back-references, for example you want any request for http://mydomain.com/stuff/ to map to /internal/stuff/:
RewriteRule ^stuff/(.*)$ /internal/stuff/$1 [L]
So requests for http://mydomain.com/stuff/file1.html, http://mydomain.com/stuff/image1.png, etc. get rewritten to /internal/stuff/file1.html, /internal/stuff/image1.png, etc.

Issues when creating pretty URL that uses actual site urls

I want to create functionality similar to the site downforeveryoneorjustme.com. They use a pretty URL to take in the URL of any given site. I sure they use htaccess to do this, however the method i'm using is encountering problems.
This is my .htaccess file that I'm using to send the site URL to a file.php:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)?$ /file.php?var=$1
However when I type in something like
mysite.com/http://google.com the variable it sends the file is http:/google.com (missing a slash). I can't figure out why this is occurring.
Also, when I type in something like mysite.com/existingfolder, where existingfolder is a folder on my site, it always works incorrectly. The variable it passes to the file is missing.html instead of existingfolder. In this case, the file doesn't display images. The image can't be found, and i'm assuming its because it's searching for the image in an incorrect folder on the site. That it might think it's in existingfolder and not in the normal folder it should be in.
Does anyone know why I'm getting these problems? I'm knew to htaccess, and I'm assuming it has something to do with that.
Thanks for any help.
I sure they use htaccess to do this
I'm not. I'm not even sure they're using Apache.
mod_rewrite is not always the answer to all URL-processing problems. It's certainly prone to some of the quirks of path-based URL handling, including the removal of double-slashes.
I suggest reading the Apache-specific REQUEST_URI variable from your script, rather than relying on rewrites to get a parameter. This will give you the path requested by the browser without any processing.

Resources