Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url Rewriting doesn't work with Apache

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!

like image 703
Jan Peter Avatar asked Jun 24 '26 11:06

Jan Peter


1 Answers

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]
like image 197
anubhava Avatar answered Jun 27 '26 04:06

anubhava