Url Rewriting doesn't work with Apache - php

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 slajaa09#htlkaindorf.at
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]

Your first rule,
RewriteRule ^/api/(.*)/(.*)$ api/$1.php?item=$2
assumes that there will be a third path element, such as /api/logout/stuff. For the case where there's no third element, you need to add another rule below it:
RewriteRule ^/api/(.*) api/$1.php

Related

How to Rewrite directory url in htaccess?

I have directory like this in my site. => aaa.com/p/folder/
How to change url to => aaa.com/folder/
Note : aaa.com/folder/ doesn't exists in my site. I tried this but get error 404 instead.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/p/(.+)$ /$1 [L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?aaa.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1
RewriteCond %{HTTP_HOST} ^(www.)?aaa.com$
RewriteRule ^(/)?$ folder/index.php [L]
Look in your conf file and make sure that AllowOverride is not set to the default of "none". I struggled with this for a while too and the .conf setting was what was causing my biggest headache.

htaccess rewrite rule for subfolder (nice url)

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.
Currently I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]
Now it works like:
http://example.com/page translate into http://example.com/?arg=page
What I would like to do in addition is this:
http://example.com/admin/page translate into http://example.com/admin/?arg=page
I don't know at all how to make that happen, can somebody help me please?
Thanks
You can use these rules with appropriate RewriteBase:
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
Or just add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]

htaccess - insert script name into all URL requests

I have an application which requires an htaccess file that contains the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
But I am getting 404 errors matching the following patterns:
http://107.170.120.88/tileserver/extra5/11/331/794.png
and:
http://107.170.120.88/tileserver/extra5/11/331/796.grid.json
It seems that any URL with the path:
tileserver/extra5/
needs to be instead:
tileserver/tileserver.php/extra5/
but I haven't had much luck writing an htaccess file to fix this.
If I take out the
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
the base script doesn't work, but then if I add something like:
RewriteRule ^tileserver/extra5/(.*)$ tileserver/tileserver.php/extra5/$1
it has no effect. How do I write this rule?
--- UPDATE ---
Here's the full htaccess:
DirectoryIndex tileserver.php
RewriteEngine on
RewriteBase /tileserver/
<FilesMatch "\.mbtiles$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteRule ^(.+).jpeg$ $1.jpg [L]
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]
RewriteRule ^tileserver.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
Note that it is sitting in a subdirectory, in which the parent (root) has this htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Which removes index.php from all URLs (for codeigniter). Maybe that's conflicting?
With the datastructure like this:
example.com/tileserver/tileserver.php
and the url for the matching pattern
107.170.120.88/tileserver/extra5/11/331/794.png
which should result in 107.170.120.88/tileserver/tileserver.php?/extra5/11/331/794.png
your .htaccess should look something like this:
#turn on the rewrite engine
RewriteEngine On
#use the as base for your rewrite conditions the following folder
RewriteBase /tileserver
#since we don't want to rewrite the requests pointing directly to this script, leave it
RewriteRule ^tileserver.php - [L]
#if its not a file or a folder rewrite everything to tileserver.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#everything that comes after /tileserver is passed as argument to the php script
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
I suspect the problem might be the [L] flag.
Instead, try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [QSA]
RewriteRule ^tileserver/extra5/(.*) tileserver/tileserver.php/extra5/$1 [L,QSA]

htaccess redirect virtual directory

Can't figure out what I'm doing wrong.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)\.php$ detail.php?name=$1 [NC,QSA,L]
RewriteRule ^(.+)/directory/\.php$ detail2.php?name=$1 [NC,QSA,L]
The first RewriteRule should redirect anything (ending on php like domain.com/product1.php) from the root domain to detail.php (it can not affect things like domain.com/contact.php)
A 2nd RewriteRule should redirect anything from domain.com/directory/product-b1.php to detail2.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /foldernamehere/index.php/$0 [PT,L]
paste this code in your .htaccess file it will resolve your problem

PHP .htaccess exception some of .php file

I have .htaccess that the function is to hide .php extension and convert user.php?user=username to be :
http://example.com/hidayurie
Now I have a problem, I have file search.php. When I run the URL : http://example.com/search?q=username. It still detect that search is username whereas I have file search.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
How can I set if .php file, then it will open that php file without extension?
Try adding some conditions to your rules to prevent the rewrite if the URI points to a file that exists. You should also make sure you have multiviews turned off (this can be anywhere in your htaccess file):
Options -Multiviews
Then add these conditions before each of these 4 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
You need to define search and any other different pages/URLs you wish to rewrite, otherwise yeah it will continue to think it's a user and refer to user.php
Example:
RewriteRule ^search/?$ /search.php
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ /search.php.php?q=$1
Second line would allow you to query your search with a clean url, ex; yoursite.com/search/username/

Categories