Hello guys. been searching the whole internet and tried every single thing I have found but i cant make it work. i would really really appreciate if somone could help me with this
i have hidden .php extensions in the url. schedule is a .php file
i simply has a webpage looking like this:
example .com/folder/schedule?c=test
but i want it to look like this
example. com/folder/schedule/test
RewriteEngine On
RewriteRule ^schedule/([^/]+)/([^/]+)$ /schedule.php?c=$1&day=$2 [L]
RewriteRule ^schedule/([^/]+)$ /schedule.php?c=$1 [L]
That means:
âscheduleâ followed by a slash, then something, then a slash and then something else must be rewritten to /schedule.php?c=something&day=something else
âscheduleâ followed by a slash, then something must be rewritten to /schedule.php?c=something
Now it depends on how youâve setup Apache to hide the .php extension - if youâve used MultiViews for that then the above wonât work because MultiViews takes precedent over rewrites.
In that case I would advise to disable multiviews using Options -MultiViews (on a separate line) and then add a rule for /schedule like so:
Standard practise with most PHP Frameworks and Content Management Systems is to redirect all browser requests to a default index.php if and only if the request is not a specific file or directory:
file: .htacces
# REWRITE STUFF
RewriteEngine On
# ONLY CALL ONCE
# <IfModule mod_rewrite.c>
# FORCE NON-WWW
# RewriteCond %{HTTP_HOST} ^www.example.com [NC]
# RewriteRule ^(.*)$ https://example.com/$1 [L,R=302]
# Force www:
# RewriteCond %{HTTP_HOST} ^example.com [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
# FORCE PRETTY-URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?$1 [L]
# </IfModule>
ErrorDocument 404 error-document.html
file: index.php
<?php
declare(strict_types=1);
// following two lines should be set in php.ini
error_reporting(-1);
ini_set('display_errors', '1');
$page = $_SERVER['QUERY_STRING'] ?? 'home';
$page = empty($page) ? 'home' : $page;
$page = explode('/', $page)[0];
switch($page):
case 'home' :
// do stuff here
break;
case 'search' :
// do stuff here
break;
case 'search-date' :
// do stuff here
break;
default:
// do stuff here
break;
endswitch;
This method is more expandable and also good for SEO link juice
Ideally you should not need to use <IfModule...>. You should know whether the module is loaded or not and either include the appropriate directives - or not.
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.
That would indicate the .htaccess causes Apache to redirect in a loop, so it rewrites a URL to something else, then again, and again, and again, etc, until Apache stops it
Thank you guys. I think i am closer now. I still get the same error in apache log but now the HTML get shown(before i got the 500 Internal Server Error over the whole page) on schedule.php, but all the style and js files seems to fail to be linked?
That makes your .htaccess file really easy to read and it will never accidentally match something you didnât mean to. Just make take into account that Apache tries URL from top to bottom, so start with the more specific URLs at the top and work your way down to the more generic.