.htaccess mod_rewrite dynamic match with Yii generated urls - apache

I'm trying to use Apache's mod_rewrite module to make dynamically generated urls from a Yii app, which always contain index.php as the router/bootstrap, into static urls through .htaccess available to browse through a web browser.
Example: localhost/projectname/index.php/commentfeed.xml is the dynamically generated page, but I want it to be able to be accessed through localhost/projectname/commentfeed.xml
Apache version I'm running is: Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.0 through XAMPP for my development platform. The mod_rewrite module is present and loaded in Apache's httpd.conf file.
My .htaccess in localhost/projectname/ is as follows:
#Turning on the rewrite engine is necessary for the following rules and features.
#FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.so>
Options +FollowSymlinks
RewriteEngine On
#Unless an explicit file or directory exists,
#redirect all request to Yii entry script.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Now, when I try to access commentfeed.xml through my browser without index.php (localhost/projectname/commentfeed.xml) I get a 404 error - Object not found!.
I've looked over the following guides & SO questions: URL Rewriting Guide, mod_rewrite documentation, and SO: Tips for debugging .htaccess rewrite rules but none of them had explicitly what I was looking for. Also, my apache error log is not reporting anything about the .htaccess file. Any help or guidance towards the correct usage would be greatly appreciated.

You need to tell the Yii UrlManager that the script name is not required. In your config/main.php file you should have:
'components'=>array(
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
),
),
),
Make sure you also have a route defined to specify the controller/action that generates the commentfeed.xml file.
More info: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x

The specific answer to my problem lay in using <IfModule> ... </IfModule>. With my setup of XAMPP v3.2.1 and the apache server installed with it, the only lines of code I needed in the .htaccess was simply:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

Related

Yii2 | Redirect to backend/frontend with htaccess

I am using Yii2 advanced template on localhost and I want to redirect the user to backend/web when the url is localhost/site/admin and redirect to frontend/web when the url is localhost/site using .htaccess file.
I enabled apache rewrite and I have tried these solutions
advance template Yii2 - redirect to frontend/web using htaccess
Yii2 .htaccess redirect to backend part
Redirecting to backend via htaccess Yii2
but in all cases I get 404 Error when the url is localhost/site/admin
Not Found
The requested URL /site/admin was not found on this server.
and when the url is localhost/site the content of the root directory of my site is displayed to me.
Now my .htaccess in the root of project is :
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
and in backend/web and frontend/web is:
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
Although I had enabled mod_rewrite through the terminal via the following command:
sudo a2enmod rewrite
But it was not enabled and I manually activated it from ‍/etc/apache2/apache2.conf.
use it without site
localhost/admin

Redirect all requests for a subfolder to the subfolders index.php with Apaches .htaccess and/or httpd.conf

All I want to do is the following:
User attempts to connect to :
http://mydomain/invites/XYZ123456
or
http://mydomain/invites/FOOFOOBLAHBLAH
or
etc ...
It must work for any URL that is entered after /invites needs to display the same /invites/index.php
I want the webserver to always show /invites/index.php
I can't seem to get this to work...
I have tried to setup the .htaccess and place directly inside the folder (/my/WebServer/www/invites... which is http://localhost/invites/)
Note: I have made sure that I have loaded the 'rewrite_module' inside the apache httpd.conf
#.htaccess (located in /my/WebServer/www/invites)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule . index.php [R=302,L]
Any help greatly appreciated.
Thank you.
Make sure you have AllowOverride set to All inside the httpd.conf to enable the usage of the .htaccess file within the (for this example /www/invites directory).
Also make sure you have loaded the rewrite_module (mod_rewrite.so)
Hope this can help anyone who may run across this post:
I ended up using the following configuration parameters inside my .htaccess
# /www/invites/.htaccess
# Internal redirection to index.php for nonexistent URLs.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /invites/index.php [L,QSA]

.htaccess rewrite url for missing reources

I would like to rewrite files that don't exist to a php handler. I am currently using this .htaccess file, but it doesn't work as I'd like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/index.php?_url=/$1
When I have no files, it works; it redirects correctly for resource that does not exist to my app, and I am able to capture it.
When I have exactly matching file (e.g.: test.txt and I request /test.txt); it loads test.txt correctly.
However, when I have a partial match (e.g.: test.txt exists, but I request /test); it does not redirect at all. In fact, it gives me the standard Apache 404. I want this to actually rewrite to my app, so I can deal with the request in a different manner.
I'm using pretty much default apache 2.2.22 from Debian. Is there some configuration I am missing, or is this the intended behaviour of the rewrite? Is there a way to achieve what I want?
I think you have MultiViews enabled in your Apache by default. Try adding this line on top of your .htaccess:
Options -MultiViews
With MultiViews Apache does its own rewrites and that usually conflicts with mod_rewrite

Yii framework: Using htaccess for site in sub directory not working on Hostgator

I'm trying to set up Yii site to be loaded from sub directory to root domain. In my root site folder I have only root .htaccess file and sub directory "subdir" which contains Yii site. I found a solution that works on my local environment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
But when I upload the site to HostGator it just does not work correctly
For example
if I use http://localhost/contact on my local environment, correct page is opened (called site/contact - site controller and contact action. I've added Yii 'contact'=>'site/contact' rule to the urlManager, so both http://localhost/contact and http://localhost/site/contact can work)
If I use http://mydomain.com/contact or (http://mydomain.com/site/contact) on HostGator, I get default index page (called site/index - site controller and default index action instead)
When I choose to access subsites directly like http://mydomain.com/subdir/contact it works fine, but it does not work if I use http://mydomain.com/contact
I guess that I need somehow to change this last rule in htaccess, but not sure how. Thanks!
I've found alternative solution that works. I was forced to use old fashioned URLs instead of paths, so I've changed my urlManager to:
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=>true,
'caseSensitive'=>false,
'rules'=>array(
...
)
)
And my root htaccess looks now like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/index.php?r=$1 [QSA,L]
So, when accessing
http://mydomain.com/site/contact
Actual mapped URL is
http://mydomain.com/subdir/index.php?r=site/contact
which as result returns correct contact page
Since, you stated in your previous question that www.mysite.com/mysite/frontend/www/controller/action works fine; you should be using:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)$ /mysite/frontend/www/$1 [L]
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

how to remove file extension from file name when hit the url in php?

I am working with WAMP server. This is my filename ("localhost/blogs.php")
but I want to call this "blog.php" file as ("localhost/blogs"). How can I remove the file extension from the URL?
If you are using an apache server the easiest way is to use mod_rewrites
Create a .htaccess file and write the following code:
# Options
Options -Multiviews
Options +FollowSymLinks
#Enable mod rewrite
RewriteEngine On
RewriteRule ^blog$ blog.php [QSA,L]
This will mean when /blog is added in the browser then the server will call blog.php
That's not a PHP issue but a mod_rewrite one. Enable mod_rewrite in Apache (probably already the case) and then add a file named .htaccess in your website's root directory and add this content to the .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
PHP frameworks use this kind of thing, I can advice codeIgniter.
Otherwise you need to create a URL handler, and use a .htacces file
This might help