I need a suggestion using .htaccess
(I presume) or any other solution for the following situation:
On the server, for example I have a folder www.mypage.com/accommodation/
which contains index.php
script.
What I would like to do is that when the called URL is something like www.mypage.com/accommodation/apartment-mike
(note no / in the end or extension), that the string "apartment-mike
" ($_SERVER['request_uri']
) is passed to the index.php
script in the accommodation folder instead of trying to directory walk apartment-mike directory.
What would be the best way to do this?
First you must rewrite the call to index.php
. This is a classic and can be found anywhere
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
The next step is accessing the URI itself, which you already figured yourself by using $_SERVER['REQUEST_URI']
.
You can add the file apartment-mike.php
, contains a ridirection to index.php
, to the accomodation
folder then add the following lines to the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
to remove the extension from appearing in the uri don't forget to add
header("location:index.php");
to the apatement-mike.php
if you want it to pass users to the index