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.
Related
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
I have the following code in my .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)$ /pages.php?link=$1
My understanding is that it should check if the file does not exist, check if the directory does not exist and then perform the rule if those are true.
For some reason though it is giving me an error when I go to:
http://localhost/1.php
The error message that pops up is:
script 'C:/xampp/htdocs/1.php' not found or unable to stat
Strangely though, when I go to:
http://localhost/1
it acts as it should (redirects accordingly).
I believe your problem is your regex. You are using the \w to match a word. Well . is not matched in that. Change your rule to this.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pages.php?link=$1
I have this directory structure:
|-language-A\
|-language-A\program-A
|-language-A\program-B
|-language-A\program-C
|
|-program-A\
Currently program-A is not translated to the language-A but somehow Google was able to crawl this directory and now it's showing in the search results. I need to redirect language-A\program-A to program-A in the root ... not all the folders inside language-A just that directory. This is a temporary redirection as I will remove that rule once that translation is ready.
So basically it will be something like:
http://example.com/language-A/program-A/what-ever
To:
http://example.com/program-A/what-ever
The question really doesn't say much so I will assume all strings are fixed and there are no parameters to pass:
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/language-A/program-A/? [NC]
RewriteRule .* program-A [R,L]
Redirects temporarily
http://example.com/language-A/program-A
To:
http://example.com/program-A
All according to your question: I need to redirect language-A\program-A to program-A in the root...
UPDATED
According to question update:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/language-A/program-A(.*)? [NC]
RewriteRule .* program-A%1 [R,L]
Redirects temporarily
http://example.com/language-A/program-A/what-ever
To:
http://example.com/program-A/what-ever
/what-ever is optional and will be passed if exists.
I'm trying to get the requested filename without the path with htaccess for a RewriteCond.
REQUEST_FILENAME returns the full absolute path, but I only need the filename like test.php
I've been searching for this a lot but couldn't find anything that helped me out.
Thanks for any responses in advance!
Edit:
Im trying to do something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond _%{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]*)$ _$1.php [L]
The URL typed in the browser looks like this: http://example.org/test
The File that will be requestested by the RewriteRule is: http://example.org/_test.php
With RewriteCond _%{REQUEST_FILENAME}.php -f i tried to check if the file exists first
Basically I want to do this:
URI: /test/blah
Check if _test.php exists (with underscore!)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/_$1.php -f
RewriteRule ^([^/]+)$ _$1.php [L]
I changed * to + so requests for e.g. example.com/ will not redirect to _.php
This is my htaccess:
RewriteEngine On
# Stop if it's a request to an existing file.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# Redirect all requests to the index page
RewriteRule ^([^/]) /index.php [L]
Now this forwards everything to my index.php script! It dosen't stop if a script exists. Anyone have any idea why this isn't working? I've looked everywhere, but I don't really understand mod_rewrite (as much as I thought!).
The problem has come about because I've put in <script> tags which point to .js files in my web directory which are then forwarded to the index.php script. The web developer toolbar tells me this. :) And clicking links to the js files in firefox's view source window also shows the index.php output.
thank you.
This is because after processing a rewrite rule the whole process restarts with the new url. The processing of an url can go thru the rules over and over again each time with the changed URL, until there is no more change (no applying rules found).
You need this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php [L]
Don't think of the rules as a program, they are rules which can overlap and must be as specific as possible with each one.
I guess you should add [OR] after the first condition like this:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]