I want the following:
myhomepage.com <- this should turn to this -> myhomepage.com/index.php
myhomepage.com/mypage <- should turn to this -> myhomepage.com/index.php?page=mypage
myhomepage.com/mypage/mymethod <- should turn to this -> myhomepage.com/index.php?page=mypage&method=mymethod
myhomepage.com/api/logout <- should turn to this -> myhomepage/api/logout.php
This is my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^/api/(.*)$ api/$1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ index.php?page=$1&method=$2&item=$3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?page=$1&method=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1
Everthing works as it should, but the rule with api doesn't seem to work.
UPDATE 1:
RewriteRule ^/api/(.*)$ api/$1.php
When I call myhomepage.com/api/logout it rewrites it to myhomepage.com/index.php?page=api&method=logout
UPDATE 2:
My first thought was, maybe there is something like a default rewrite_module. Which rewrites everything what's not a file, to index.html or something like that. Especially when I use XAMPP.
UPDATE 3:
After experimenting a little bit, I found out, that I can't call it with api because it is a directory. When I rewrite this for example with myhomepage.com/ap/logout and RewriteRule ^ap/(.*)$ api/$1.php it works fine. Any ideas how to get this work with the directory name?
UPDATE 4:
I redirect the link todo.js (from host in Windows/System32/drivers/etc) to 127.0.0.1
then I edited the httpd-vhosts.conf and added the following:
<VirtualHost todo.js:80>
ServerAdmin [email protected]
DocumentRoot "C:/xampp/htdocs/todo"
ServerName todo.js
ServerAlias todo.js
</VirtualHost>
And my api folder is a subdirectory of htdocs/todo
*SOLUTION: *
This is the final solution for my problem:
ErrorDocument 404 /error.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule api/([^/]+)/?$ api/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/(.*)$ index.php?page=$1&method=$2&item=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.*)$ index.php?page=$1&method=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
Special thanks to anubhava!
Leading slash is not matched in .htaccess because .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
Use these rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule api/([^/]+)/?$ api/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(.*)$ index.php?page=$1&method=$2&item=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ index.php?page=$1&method=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With