Rewriting URLs and "faking" folders - mod-rewrite

I'm trying use mod_rewrite to rewrite URLs from the following:
http://www.site.com/one-two-file.php
to
http://www.site.com/one/two/file.php
The folders don't exist, but "virtually" exist for the rewriting purpose.
What rule do I used in this?

Untested:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)\.php$ $1-$2-$3.php [L]
I can't really understand your explanations about virtuality and existence: one-two-file.php must exist or you'll have nowhere to redirect to.
Update
The previous version works fine when used from an .htaccess file. However, if used from main http.conf file you need to add leading slashes:
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)\.php$ /$1-$2-$3.php [L]
I presume that's why it wasn't working for the OP (he was probably getting a 404 not found status code).

Related

.htaccess will not redirect .php files

I have this in my htaccess, standard domain redirection:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ "http\:\/\/fullnewdomain\.org\/$1" [QSA,R=301,L]
And it works fine for folders, subfolders, html files, images, etc. However, for some reason it refuses to redirect php files. Instead they still run as normal and do not redirect to the new domain. Any ideas as to why, and how I can fix it? It's almost like this host is trying to execute the php file before checking any rules ( And I'm not sure what I could do if that's the case! ).
Turns out there was an htaccess file the host had placed in the home directory ( One level above public_html ) to "counter" bots, but all it did was break anything else that tried to apply a rule to php files. Removed the file, problem solved itself.
For reference, hostgator was the host, and I still don't know why they felt the need to place the file there in the first place.

mod_rewrite to parent directory

I would like to create a .htaccess file that would do this logic:
If the requested file is not found try to find it in the directory
above.
I don't want to redirect the browser I would just like to internally rewrite the request.
I tried and searched for this a lot but always got stuck because (as I gather from the log) the where I could do the rewrite the path was always already without its per directory prefix. In the example below the .htaccess file is in the lang folder. If the lang specific file is not found it should just take the file from the parent folder. I understand that it is possible to do it by hardcoding the parent directory or by placing the .htaccess higher, but now that I suffered for so long in trying I would be very interested to learn if it was possible at all this way.
strip per-dir prefix: X:/localhost/htdocs/peopletest/public/img/root_cli/lang/en/loginhead.gif -> en/loginhead.gif
applying pattern 'somePattern' to uri 'en/loginhead.gif'
Thanks for the help.
SWK
Like this?
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-l
RewriteRule /[^/+]/([^/]+)$ $1

mod_rewrite from a subfolder with a querystring to a folder or file

I am trying to redirect from:
http://www.example.com/folder/product.aspx?prodid=146
to
http://www.example.com/folder2/folder3/
The folders referred to here don't really exist. There are other rewrite rules in place which redirect transparently to the actual content.
If I create a directory called 'folder', and put an .htaccess file in it, I can get the redirect working, BUT, other URLs which refer to that folder no longer work. So I have to try and do the redirect from the .htaccess file in the ROOT folder.
I tried this:
RewriteCond %{QUERY_STRING} prodid=146
RewriteRule ^/folder/product.aspx$ /folder2/folder3/? [R]
...but it doesn't work (I get a 404 error). Using identical syntax but omitting the /folder/ from the 2nd line works if the .htaccess is in the folder directory (so I know the above can't be too far off) - but as I said, I cannot do that. I have tried lots of variations but nothing seems to work. Any assistance appreciated.
You need to remove the slash from the start your URL regexp. Like this:
RewriteRule ^folder/product.aspx$ /folder2/folder3/? [R]

Ko3 - URL Rewriting problem - Removing index.php

I'm developing a website using Kohana 3 (1rst time I use a framework). Locally, everything works perfectly. At the moment, I have a default template controller, a multi-language support and my 'index.php' is correctly removed. So before going further, I tested if it worked on my server and I got an endless loop.
I followed the tutorial from the unofficial wiki for the multi-language implementation: http://www.kerkness.ca/wiki/doku.php?id=example_of_a_multi-language_website
A redirection to the default language occurs if the language is not specified in the uri so I figured the problem might have come from there even though it worked locally, so I removed it to see what happens without the redirection. Now, I can see my home page, but whatever the uri is in the web browser, the home page will always be called. I inserted the following line in my home view to check what the uri was:
request::instance()->uri() and effectively, the uri is always: /en/home/
I put the index.php back (in the bootstrap) and everything worked fine again, even with the redirection to the default language.
My first guess was that the uri isn't rewritten correctly, so I tried to change the .htaccess but no success...
Here's my .htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /dev/
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system)/ - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
(btw I also tried the other RewriteRule in the unofficial wiki, doesn't work either)
Additional info:
Host: WebHostingPad
Apache: v2.2.11
PHP: 5.2.9
Rewrite_Module is activated
Thank you, I would really appreciate your help because I've been trying to fix this for days now and it's really starting to annoy me ;)
The only thing you have to change in order to get rid of index.php in URL is to set the 'index_file' param in Kohana::init ( bootstrap.php ) to FALSE ( everything else can cause an error ).
So the Kohana::init looks like this;
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE,
));
If it worked with the original .htaccess, there's no need to change it at all.
The problem came from $_SERVER['PATH_INFO'] which returned no value...
This issue can be solved by adding the following line to the php.ini:
cgi.fix_pathinfo=0

What's wrong with my RewriteRule?

My .htaccess file currently looks like this
AddType x-mapp-php5 .php
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^account$ /account/orders.php [L]
When I go to http://mywebsite.com/account it properly shows the page at http://mywebsite.com/account/orders.php. But when I change the RewriteRule to
RewriteRule ^account/orders$ /account/orders.php [L]
and then I go to http://mywebsite.com/account/orders, I get Error 404 Page Not Found. What did I do wrong?
******Additional Details**
I finally diagnosed the problem. But I don't understand why my solution works. Consider the scenario where account/orders.php exists.
The following rule will not work
RewriteRule ^account/orders$ account/orders.php [L]
The following rule will work
RewriteRule ^account/order$ account/orders.php [L]
Ie., the rewrite rule will fail if the Pattern evaluates to an existing file. So when the pattern is the same as the substitution, but minus the extension, the rewrite rule will fail. If I add a file called account/order.php, then both rules will fail.
Why does this happen?
I don't see how your first example would work, because I believe that intial slashes are also passed on.
RewriteRule ^/account/orders$ /account/orders.php [L]
Have you tried a relative path?
RewriteRule ^account/orders$ account/orders.php [L]
Edit   You should also make sure to have MultiViews disabled. This causes that Apache does some additional vague file matching to find similar named files and thus /account/orders would be mapped to /account/orders.php before it’s passed to mod_rewrite.
Strange, it seems ok to me...
If you have access to Apache Configuration try to enable RewriteLog and RewriteLogLevel for some debug...
Also take a look in the site's log files (always if you have access)
I would at first try to add a redirect to my rules, so I can see in the browser what is happening on the server.
RewriteRule ^account$ /account/orders.php [L,R]
Also make sure that there are no other rules (previous ones) interfering, just in case you are not showing all of your .htaccess file here.
I'm answering my own question because I finally diagnosed the problem. But I don't understand why my solution works. Consider the scenario where account/orders.php exists.
The following rule will not work
RewriteRule ^account/orders$ account/orders.php [L]
The following rule will work
RewriteRule ^account/order$ account/orders.php [L]
Ie., the rewrite rule will fail if the Pattern evaluates to an existing file. So when the pattern is the same as the substitution, but minus the extension, the rewrite rule will fail. If I add a file called account/order.php, then both rules will fail.
Why does this happen?

Resources