Creating symlink gives 500 Internal Server Error - .htaccess

Applied cmd: ln -s subdir
Edits to .htaccess: added this chunk between RewriteBase / and first RewriteCond line :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} ^/subdir
RewriteRule ^ subdir/index.php [L]
Loading the page (path e.g. www.example.com/subdir) gives 500 Internal Server Error. The log gives: 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. But I don't get it at all, where is it pulling that 10 redirects from?
The website is www.example.com and is running in one db (on Drupal). You can have multisites while running on same root install of CMS and database (but with prefixes). So the new website should open when visiting www.example.com/subdir (its FTP dir is htdocs/sites/example.com.recruiting) More infos: https://drupal.org/documentation/install/multi-site#subdirectory, but like I mentioned before, applying the .htaccess part ends up in internal server error.

Your conditions should mean rewrite if not already /subdir but it is doing reverse.
Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(subdir|favicon\.ico)
RewriteRule ^ subdir/index.php [L]

Related

Rewrite url with query parameter

I would like to rewrite a specific url and remove the query parameter. However online I can't seem to find a solution which works for me. I'm pretty new to .htacces files and the working of them.
My file for now, which is in the main folder of my website:
ErrorDocument 404 /404.html
#Remove php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Remove html extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
#Remove id query parameter
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^webhook/(.*)$ webhook.php?q=$1
So the normal url is: https://example.com/api/webhook.php?id=10
which I want rewrite as: https://example.com/api/webhook/10
Does anyone know how to create this for this specific case? Thanks in advance!
EDIT:
In my webhook.php file I have this for testing purposes:
<?php echo $_GET['id']; ?>
When I go to the url https://example.com/api/webhook.php?id=10 I get output 10 in the browser.
When I go to the url https://example.com/api/webhook/10
I get this error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
And in the serverlogs I found this error:
Request exceeded the limit of 4 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace
Seems there are too many rewrites.
SOLVED
I found that these lines fixed my problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/webhook/(.+)$ api/webhook.php?id=$1 [L]
Now when I go to https://examply.com/api/webhook/10 it loads the webhook.php script with query ?id=10. The page now outputs '10'.
RewriteEngine On
RewriteRule ^api/webhook/([0-9]+)/?$ api/webhook.php?id=$1 [NC,L,QSA]
Usage = These rules will make your url https://example.com/api/webhook.php?id=10 to https://example.com/api/webhook/10

Htaccess redirection not working, server error

I am facing a problem 500 server error, I try to redirect the user to a different URL depending on his country of origin using the .htaccess file and GEOIP, I use Codeigniter 3.
Error 500 : Is [Fri Jul 15 15:37:32.432374 2022] [core:error] [pid 3029:tid 140071902476032] [client 86.235.179.208:0] 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.
I would like to do this : https://example.com ----> https://example.com/fr/
Here's my script before adding the code :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
and after adding the code :
RewriteEngine On
RewriteCond %{HTTP:X-FORWARDED-COUNTRY} ^France$
RewriteRule ^([^/]+)$ /fr/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Thanks in advance for your help.
The comments suggest that you are actually trying to redirect requests in a country specific manner. That is not what you implemented, though.
Take a look at this slightly modified variant:
RewriteEngine On
RewriteCond %{HTTP:X-FORWARDED-COUNTRY} ^France$
RewriteCond %{REQUEST_URI} !^/fr/
RewriteRule ^ /fr%{REQUEST_URI} [R=301,END]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php?%{REQUEST_URI} [L]
The modifications should actually externally redirect requests without the /fr/ prefix if applicable. The rewriting loops is prevented because of the additional condition preventing the redirection if it already has been applied in a previous round trip. Not that I did not test this, I just sketched it here. You may have to adjust it to your specific setup.
Remember to always test using a fresh anonymous browser window. Also using a R=302 temporary redirection is a good idea and to only change that to a R=301 permanent redirection once your are satisfied. That prevents nasty caching issues on the client side.
You can implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess") which should be located in the DOCUMENT_ROOT folder of your http host.

.htaccess - Clean URLs prevent 500 internal server error

I have this .htaccess file (which I got from other answers on here and on the web):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ ./$1.php
However, it seems that if the user manually enters an address that doesn't exist, it return a 500 Internal Server error. Any way I can prevent this? Thanks.
Try replacing this condition:
RewriteCond %{REQUEST_FILENAME}.php -f
with
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
because %{REQUEST_FILENAME} checks for path info and other possible matches for scripts. This circumvents adding a .php to the end of the URI.

Rewrite subdomain to subfolder causes loop error or 403

I have a local Apache server set up on my machine, with wildcard DNS in place. I have it set up so that it works like [foldername].loc. So, for instance, a folder under my htdocs folder called MyDomain, would be accessed via mydomain.loc. This code works fine, and the code in my .htaccess in my htdocs is below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]*\.)?([a-z0-9-]+)\.loc$ [NC]
RewriteCond %3::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule (.*) /%3/$1 [PT,QSA]
Now, the above code also passes through subdomains, such as "john.mydomain.loc". Now, I have the following folder structure in the folder MyDomain:
MyDomain
- active
- index.php
- working
- index.php
.htaccess
In the .htaccess of MyDomain is the following code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteRule ^(.*)$ /active/$1 [L]
What this should do, if I understand correctly, is take http://live.mydomain.loc/ and rewrite it to be http://mydomain.loc/active/. Note that I said rewrite, not redirect.
With the code above, however, I get a message in the Apache error log:
[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.
If I change the .htaccess of MyDomain to read as follows:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /active/$1 [L]
When I use this code, it always comes up with a 403 error, saying I don't have permission to view the folder /mydomain/. If I set Options +Indexes, I only see the folder index of /mydomain. So where along the line is the above code failing?
I have also tried the above code with RewriteCond %{REQUEST_URI} !^/active/. This has made no difference in the results.
I have tried this for over two days, and I can't figure it out. I hope the brilliant minds of StackOverflow can help figure this out. :)
Try setting the rewrite base to where the file actually is:
RewriteEngine On
RewriteBase /MyDomain/
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ active/$1 [L]
And making the /active/ relative: active/
I solved this using the code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/active/
RewriteRule ^(.*)$ active/$1 [L]
The primary fix was RewriteCond %{REQUEST_URI} !^/mydomain/active/. The difference is, the first line I tried, RewriteCond %{REQUEST_URI} !^/active, would match /access/index.php, but not /mydomain/active/index.php.
I hope this helps someone else.

500 Internal Server Error instead of: 404 - Not Found

Apparently these lines in my .htaccess cause the server to output a 500 instead of a 404 error which should appear when trying to access a non-existant site.
RewriteEngine On
# Don't rewrite requests to /de or other real files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/(css|scripts)
# Rewrite incoming requests to their equivalent behind /de
RewriteRule ^(.*)$ de/$1 [L,QSA]
Alas I'm very unfamiliar with .htaccess. Where's the mistake in this one which causes the 500 instead of the 404 error?
This is the error in the logfile:
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.
Edit:
RewriteRule ^(.*)$ de/$1 [L,QSA]
This part is responsible for the error. Why does it prevent the 404-error page though?
This part is responsible for the error. Why does it prevent the 404-error page though?
The reason why this isn't producing a 404 error page is because of the conditions which prevent the blind match from being rewritten to de/.
These are the 3 preventative conditions you have:
# these prevent rewriting if the URI doesn't point to a resource
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# this prevents scripts from being rewritten
RewriteCond %{REQUEST_URI} !^.*/(css|scripts)
However, nothing is preventing a rewrite if, for example, /de/blahblah doesn't exist. Thus if someone requests /blahblah, this is what happens:
URI = /blahblah
checks first condidion: blahblah is not a directory
checks second condition: blahblah is not a file
checks third condition: blahblah isn't a css or script
rewrites to /de/blahblah
Rewrite engine loops, URI = /de/blahblah
checks first condidion: de/blahblah is not a directory
checks second condition: de/blahblah is not a file
checks third condition: de/blahblah isn't a css or script
rewrites to /de/de/blahblah
etc. etc.
You need to add an additional set of conditions to rewrite IF the target exists:
RewriteCond %{DOCUMENT_ROOT}/de%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/de%{REQUEST_URI} -d
Thus, you should have something like:
RewriteEngine On
# Don't rewrite requests to /de or other real files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/(css|scripts)
# make sure rewrite target actually exists as a file or directory
RewriteCond %{DOCUMENT_ROOT}/de%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/de%{REQUEST_URI} -d
# Rewrite incoming requests to their equivalent behind /de
RewriteRule ^(.*)$ de/$1 [L,QSA]
This way, a 404 will get returned because nothing is ever rewritten into the de/ directory.

Resources