.htaccess url rewrite with get parameters - .htaccess

I have an issue with my htaccess that will make me completely crazy.
I would like to replace event.php?id=158 by event/158
and event.php?id=158&display=documents by event/158/documents
I have found lot's of example doing this but it doesn't works in case and I do not understand why.
If I my rule is
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^event([0-9]+)$ event.php?id=$1 [L]
Then the id paramter is in dump($_REQUEST)
Array ( [id] => 158 [SERVERID94994] => 134038 [PHPSESSID] => 70ed00f7c3854afa31a0d8d5d4a02840 )
But with
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^event/([0-9]+)$ event.php?id=$1 [L]
Then id parameter is no more available in $_REQUEST
Array ( [SERVERID94994] => 134038 [PHPSESSID] => 70ed00f7c3854afa31a0d8d5d4a02840 )
=> Missing id
Please can you explain me what's wrong with the second rule?
Thanks

Add this to disable MultiViews:
Options -MultiViews
The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:
If the
server receives a request for /some/dir/foo and /some/dir/foo does not
exist, then the server reads the directory looking for all files named
foo.*, and effectively fakes up a type map which names all those
files, assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It then
chooses the best match to the client's requirements, and returns that
document.
Use:
options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^event/([0-9]+)$ event.php?id=$1 [L]

Related

redirect a set of URLs to another URL with part of the original in the new URLs parameters

Say I have a set of URLs
Http://www.example.com/some/thing/*
Call the bit in the star, $id
And I want it to simply return the file at http://www.example.com/some/thing.html?src=$id without telling the client its a new URL.
After some research I have been told to set up a .htaccess file, however, Ubuntu suggests that is a bad idea and that I should do it through the main configuration. I dont mind doing it either way. However, its worth noting, that for so e reason I don't have a default.conf file in my /etc/apache2/sites-available
Enable .htaccess if it is not already enabled and then you can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^some/thing/(.+)$ some/thing.html?src=$1 [L,NC,QSA]

Kohana 3.3 Routing Issue

This route I am using works perfectly on local MAMP server, but not Dreamhost or HostPapa. I figured it was just a case sensitivity issue, but from what I can tell everything looks fine.
Error Message
Kohana_HTTP_Exception [ 404 ]: The requested URL panel/asset/warranty/edit was not found on this server.
Route:
Route::set('panel/asset', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => 'panel/asset',
'controller' => 'warranty',
))
->defaults(array(
'action' => 'edit',
));
Controller: Controller/Panel/Asset/Warranty.php
class Controller_Panel_Asset_Warranty extends Controller_Site_AdminTemplate
.htaccess
# Turn on URL rewriting
RewriteEngine On
Options -MultiViews
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
SetEnv KOHANA_ENV DEVELOPMENT
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Required for dreamhost
RewriteCond $1 !^(index\.php|public|user_guide|robots\.txt)
RewriteRule .* index.php?$0 [PT,L,QSA]
Am I missing something obvious? Routing always gives me such grief... /annoyed
If you want a parameter to be a hard-coded string, do not put it in the regex array. Only use the regex array when you actually use some regex feature in the values.
This would be better. You could use the regex array to make sure id are digits, which is a valid reason to use it.
Route::set('panel/asset', 'panel/asset(/warranty(/<action>(/<id>)))')
->defaults(array(
'directory' => 'panel/asset',
'controller' => 'warranty',
'action' => 'edit',
));
This won't help you with the problem at hand though, if the URI haden't matched a route the exception read Kohana_HTTP_Exception [ 404 ]:Unable to find a route to match the URI: panel/asset/warranty/edit.
The exception is telling you Kohana is looking for a route to match the URI panel/asset/warranty/edit I assume that is what you want. So the problem would be somewhere in your application, not the .htaccess file.
There are only two methods which have the following block of code in it.
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $this->request->uri())
)->request($this->request);
Those methods are Request_Client_Internal::execute_request() and Controller::execute().
Have you placed some kind of debug statement in Controller/Panel/Asset/Warranty.php to see if it is found and executed? Because while reproducing I somehow the file was executed but class_exists('Controller_Panel_Asset_Warranty', FALSE) returned FALSE. When I copied the class name from your question and replaced the one I typed myself with it it suddenly worked. If I undid the paste it stopped working again. I checked letter for letter and my editor showed me the exact same thing before and after.
I hope this helps you narrow it down.

mod_rewrite and dynamic pages

I tried following a tutorial on the internet about mod_rewrite but it wasn't really for me. I created a .htaccess file that has the following code for now:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
From what I understand this is the basic setup of .htaccess to rewrite urls, followed by the instructions... what and how to change. I tried different exampled but it didn't worked for me. I have a dynamic page with the url localhost/alpha/oferta.php?id=52042156c65d4, where id="..." is the unique id of that offer. I want to change it to localhost/alpha/oferta/id=".."
Can you please show me an example of how can I achieve that? Also if you know any helpful tutorials let me know. Let me know before downrating so I can edit my question. Thanks!
So you want this kind or URL : localhost/alpha/oferta/id=123abc to be redirected to localhost/alpha/oferta.php?id=123abc.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^alpha/oferta/id=([A-Za-z0-9]+)$ alpha/oferta.php?id=$1 [L]
Remember a few things :
this won't magically change "old" URLs into "new" ones. You must use rewritten ("new") URLs everywhere. Then your htaccess will change this readable URL into a technical one, which can be used by your code.
this redirection is transparent. If you want the URL to change into the browser bar, use [L,R=301] instead of [L].
this will only accept letters (case insensitive) and numbers for your id.
you can find a good cheat sheet about mod_rewrite here.

.htaccess RewriteRule returns a blank page

I'm trying to solve this for more than two hours now.
I have a personal site which uses .htaccess to manage urls.
It looks like this:
RewriteEngine on
RewriteBase /
...
RewriteRule ^sklad/?$ index.php?action=sklad
RewriteRule ^sklad/user/([0-9]+)?$ index.php?action=sklad&user=$1
RewriteRule ^sklad/folder/(.+)?$ index.php?action=sklad&folder=$1
RewriteRule ^sklad/file/(.+)?$ engine/ajax/sklad.php?file=$1
RewriteRule ^sklad/logout/?$ index.php?action=sklad&op=logout
...
RewriteRule ^admin/?$ admin.php
RewriteRule ^admin/news/?$ admin.php?action=news
the first five ones work fine. The admin/ one works fine. But when I try to access admin/news/, I get a blank page. No errors displayed or logged by Apache, and no output. admin.php?action=news is working fine.
Both sklad/ and admin/ folders physically exist on the server. BUT when I rename the admin/ folder to something else OR change the the last RewriteRule to something like
RewriteRule ^admin123/news/?$ admin.php?action=news
I can access admin123/news/. If it has something to do with the actual folder existing on the server, then why the first five rules are working? This doesn't make sense.
I'm out of ideas, hope someone here helps...
Yes, it's called news.php... I renamed the file and exerything is fine now, thanks! Didn't know about this, pretty unobvious bug (or not?)
It's not a bug, it sounds like content negotiation (via mod_negotiation) is turned on and it's doing something you don't want. Negotiation can be turned on via a type map or the MultiViews option. Typemaps are a little explicit to setup, so I'm assuming since you don't know why this is happening, you haven't set of a specific type that maps to news.php. So you've probably got Multiviews turned on. You can turn it off by either removing it from an Options statement:
# remove this word -----------v
Options Indexes FollowSymLinks Multiviews
This could be anywhere, in your htaccess, server config, vhost config, some config include file, etc. So you can also explicitly unset it in your htaccess file (as long as you aren't also explicitly setting it in the same file):
Options -Multiviews
I am not to good with htaccess and RegExp but i think admin/news will fall into your first htaccess rule.
RewriteRule ^admin/?$ admin.php
It wont proceed on your second rule on admin which is:
RewriteRule ^admin/news/?$ admin.php?action=news
Its the same problem I was facing before.
Try to modify your admin.php, echo/print something when there is no params pass. Something like this:
if($_GET['action'])
{
echo 'With Parameters';
}
else
{
echo 'No Parameters pass';
}
Debugged it that way.

RewriteRule variables blank

I have a couple of rewrite rules in htaccess. They work on one server but not another. My script is as follows (I've commented out how the urls look):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/images/
#example.com/regions/fife/
RewriteRule ^regions/([A-Za-z0-9\-\+\']+)/?$ /regions.php?region=$1 [L]
#example.com/regions/fife/dunfermline
RewriteRule ^regions/([^/]+)/([^/]+)$ /regions.php?region=$1&town=$2 [L]
It returns two variables (region & town) I can manipulate in PHP, and throw up the appropriate content. I have a Rackspace server, and the script works perfectly, but on another server (Freedom2surf) it only works so far. It doesn't return the variables. I get a blank $_GET array...
Any ideas? F2S aren't giving me any clues, just that I should check my code. But if it works on another server, then what gives? Is it an Apache setting that is different?
I think you may be after the 'QSA' flag, which will append the query string from the original request to the redirected request, e.g:
#example.com/regions/fife/
RewriteRule ^regions/([A-Za-z0-9\-\+\']+)/?$ /regions.php?region=$1 [L,QSA]
This sounds like you have a mod_negotiation conflict here and you need to turn Multiviews off. Sometimes apache default configurations have Multiviews turned on by default. What that does is it will look at a request, say, /regions/1234 and mod_negotiation will notice that there is a file /regions.php and assume that the request is actually for that php file. It'll thus serve /regions.php/1234 and completely bypass mod_rewrite. You can use Options to turn it off. Just add this to the top of your htaccess file:
Options -Multiviews

Resources