I've been searching on the web but nothing seems to work. I'm working in local with Apache and Xampp. I have created an .htaccess file and rewrote all urls ending with .php and .html to remove the extension and I have created a custom 404 page. Everything worked fine. However, now I'm trying to rewrite a dynamic url that looks like
/TotinCoblan/update/Wine?id=2
to
/TotinCoblan/update/Wine/2
and I keep getting error 500. this is my .htaccess file. From what I understood,
RewriteCond %{REQUEST_FILENAME} !-f
should avoid infinite loops.
Any help would be much appreciated.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]
ErrorDocument 404 TotinCoblan/errors/404
Try this code:
ErrorDocument 404 /TotinCoblan/errors/404
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I finally used this code, I had to use RewriteCond %{REQUEST_FILENAME} !-f since the page was loading only after reloading two time. With that instructions loads the first time.
RewriteCond %{REQUEST_FILENAME} !-f
ErrorDocument 404 /TotinCoblan/errors/404
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [L]
RewriteRule ^update/Wine/([0-9]+)/?$ update/Wine.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
You should change your -f checks to use %{DOCUMENT_ROOT}${REQUEST_URI} instead. The problem with %{REQUEST_FILENAME} is that mod_rewrite will partially attempt to map the URI to a file, and it accounts for PATH INFO, which means if the request is something like:
/something/foo/anything/
and you happen to have an existing file/script at:
/something/foo.php
then the RewriteCond %{REQUEST_FILENAME}\.php -f check will be true, and the URI that it assumes you meant is:
/something/foo.php/anything/
And, like your code is doing, the .php is appended to the end instead:
/something/foo/anything/.php
and that causes the infinite loop. Try something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]
ErrorDocument 404 TotinCoblan/errors/404
Related
The code is mixed to I have .html and .php in the same folder. There is never a time there there is the same name but different extensions. I have some code that should work but doesn't. The html part works but when I try to request a PHP page without an extension the URL it gives a 404
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -F
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -F
RewriteRule ^(.*)$ $1.php
You can try this on the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
I have the following .htaccess:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [C]
RewriteCond %{REQUEST_FILENAME} ^(.*).php$
RewriteRule ^(.*)$ /index.php [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I want it to firstly reroute everything to HTTPS.
Then I want to check if the user is accessing a PHP file, such as domain.com/pages/page/a.php and route that to /index.php.
Then lastly I want everything thats not a file or a directory to route to index.php such as domain.com/my-fun-page
I think the first and the last blocks are correct, but for some reason when I run a test on htaccess tester It works successfully. However when I run it on my own server it still lets me access domain.com/pages/page/a.php.
How can I find out why, or what am I doing wrong?
Try with below for php part,
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^ /index.php [NC,L,QSA]
You can combine your 2nd a 3rd rule into a single rule like this:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,LE]
RewriteCond %{REQUEST_FILENAME}\.php -f [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Even RewriteCond %{REQUEST_FILENAME}\.php -f [OR] may not be necessary as !-f and !-d will already cover this case.
My .htaccess is configured with the following code so that prohackr112.tk/page1.html and prohackr112.tk/page2.php are accessible via prohackr112.tk/page1 and prohackr112.tk/page2.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
But when I try to access a directory that is really an html file (e.g. prohackr112.tk/page1/indir), I get a 500 internal server error. Why is this not simply a 404 error? If this is a problem with my .htaccess, how can I configure it so that it is a 404 error?
EDIT:
When I try to go to prohackr112.tk/page.html/indir it just gives me page.html.
You can try your rules like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
The rewrite rule for the api/list/episode comes up with an Internal Server Error I'm not sure why but the other rule works but I have tried to change
this
http://domain/api/list/episode?season=1
to this
http://domain/api/list/episode/1
Is it is right for me to place the .htaccess in my root for all directory changes?
That is because of looping. Change that rule slightly:
RewriteEngine on
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^api/list/episode/([^/]+)/?$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Try changing the order of your rules. The two /api/ rules should go before the routing-to-php-extension rule. This is because %{REQUEST_FILENAME} actually inspects inner path elements so it could mistaken /api/list/episode/1 as /api/list/episode.php/1 as "exists", then you tack on a .php to the end, which makes it: /api/list/episode/1.php. It goes through the rewrite engine and the same thing happens again, and then the loop goes on and on.
RewriteEngine on
RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You can also try changing you -f check to:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ product.php?ref=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /category.php?sub=$1 [L]
The values sent with variable sub conflicts with values from ref
product.php?ref=$1 [L]
category.php?sub=$1 [L]
My advice for any kind of rewrite rules that intend some form of semantic URLs, that is:
/home
instead of
index.php?page=home
Make one rule that captures all URLs, like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ handler.php?__url=$1 [QSA]
Then have handler.php figure out what you want to do.
You know your categories and products, so if the URL is /[product|category] then handle it.
With little bit of change following code should work:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# don't do anything for a valid file or directory
RewriteCond %{ENV:REDIRECT_STATUS} !^$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Product handler /i/test
RewriteRule ^i/([^/]+)/?$ /product.php?ref=$1 [L,NC,QSA]
# PHP handler
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ /$1.php [L]
# category handler
RewriteRule ^([^/]+)/?$ /category.php?sub=$1 [L,QSA]