htaccess - insert script name into all URL requests - php

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]

Related

How can I remove a single get parameter and change it to just a slask in PHP?

So my url is like this: http://localhost/gender/?name=robert
What I wanted to do is to remove the name value pair and make it just like http://localhost/gender/robert
I googled and got this one https://stackoverflow.com/questions/21523311/removing-get-parameter-in-htaccess but it seems to complicated for me.
Any help would be much appreciated.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?name=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^robert$ gender/?name=robert&%{QUERY_STRING} [NC,L]
If you want this to work for all pages i.e. /any-page gets served as index.php?page=any-page then use
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ gender/?name=$1&%{QUERY_STRING} [NC,L]
How do these rules work?
A RewriteRule has the following syntax
RewriteRule [Pattern] [Substitution] [Flags]
The Pattern can use a regular expression and is matched against the part of the URL after the hostname and port (with the .htaccess placed in the root dir), but before any query string.

Remove /index.php/ part of CodeIgniter URLs

I tried to get rid of "index.php" in url using the following rewrite code.but it's not working
please help me to fix this bug
# Development
RewriteEngine On
RewriteBase /mvcTestApp/blog/ciBlog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
In .htaccess file, add the following.
#Rewrite index.php
#Start using rewrite engine
RewriteEngine On
#Rewrite condition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Whenever index.php is there in the url, it will rewrite to / automatically
RewriteRule .* index.php/$0 [PT,L]
Check this link for more detail: http://subhra.me/remove-index-php-urls-codeigniter/
This htaccess is working for me.Try this.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
Use this rule
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
missing "/" before "index.php" in last rule
or refer URL
http://ellislab.com/codeigniter/user-guide/general/urls.html
Here's my suite of rewrite rules for CodeIgniter. Please read the inline comments:
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Remove the "index.php" part of URI
# Remove access to "codeigniter" and "data" folders
RewriteCond %{REQUEST_URI} ^(codeigniter|data).*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Block access to "hidden" directories whose names begin with
# a period. e.g. .git, .svn
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
# WWW redirect
RewriteCond %{HTTP_HOST} ^(?!www\.).*
RewriteCond %{HTTP_HOST} ^(?!dev\.).*
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the root index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

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 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

.htaccess file extension and $_GET rewrite

i want to rewite these in my .htacces file:
/*.php -> /*(/)
(e.g. gallery.php to /gallery or /gallery/)
/snippets.php*?s= -> /snippets/*
(e.g. snippets.php*?s=test to /snippets/test or /snippets/test/)
my code so far:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^snippets/([^/\.]+)/?$ snippets.php?s=$1 [L]
the bugs that appear using my code:
/snippets/ and /snippets/test(/) will alert an 500 Error. /snippets works fine.
what am i doing wrong?
Like Micheal said, you need to change the order, however Michael didn't move the RewriteCond's, which result in the unexpected behavior.
RewriteEngine on
RewriteBase /
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
I verified this code on my test server, just to be sure.
You almost have this correct. To take specific action for /snippets, it will need to come before the catch-all rule. Otherwise the first rule matches and routes to snippets/test.php which doesn't exist.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match snippets first...
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
# Then the catch-all for remaining matches
RewriteRule ^(.*)$ $1.php [L]

Redirect two files with htaccess

this is my scenario. My application urls are showed like this:
example.com/index.php/article/whatever/here
in order to remove the index.php from url I have put this in my .htaccess and works fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my problem is that I have include and admin.php like this:
example.com/admin.php/manage/whatever/here
And I don't know how to remove the admin.php from url without make conflics with the index.php because I have tried this and a 500 Internal Server Error is showed:
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /admin.php/$1 [L]
EDIT
Another try was this and nothing, because a 404 error is showed:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
ALL my urls should have several parameteres as requiered:
example.com/this/is/a/large/url/because/is/requiered
EDIT 2:
I have create two rules, each rule alone works fine!!! but both can't work together:
RewriteRule ^admin/(.*)$ /admin.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} ^admin/(.*)$ [NC]
RewriteRule ^(.*)$ /admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then at least test this with:
http://www.example.com/some/url
http://www.example.com/admin/some/url
You can slightly modify your rewrite rule to capture the first part of the path, and use it in the replacement. Try this:
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
Basically the () create a capturing group, so anything between the first two / will be captured as $1 and anything after that will be captured as $2
Note that you can also change your rewrite conditions, if you only want this replacement to apply to index.php and admin.php

Categories