I am using ubuntu 12.04, and working for url friendly in yii framework, I follow these steps:
step 1. open the rewrite load modules by terminal
-sudo a2enmod rewrite
-sudo /etc/init.d/apache2 restart
Loaded Modules in apache:
core mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic
mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user
mod_autoindex mod_cgi mod_deflate mod_dir mod_env mod_mime mod_negotiation mod_php5
mod_reqtimeout mod_rewrite mod_setenvif mod_status
step 2: create .htaccess and copy to project beside protected
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
step 3: open urlManager in main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'urlSuffix'=>'.html',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
but I have error url when access.
Not Found
The requested URL /news/news/index.html was not found on this server.
Apache/2.2.22 (Ubuntu) Server at localhost Port 80
project news and controller news
thankyou very much
The .htaccess needs to go in the same folder as where your index.php is and the servers' document root also needs to point to this folder. You should try that /news/index.php?r=news/index works on its own first.
Related
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
I'm trying to test some simple rewrite directives in .htaccess file on my test environment with Apache and PHP-FPM.
It seems that a simple rewrite rule works well for any type of file but not for *.php files.
This works :
RewriteEngine On
RewriteRule ^sandbox/video\.html?$ http://www.google.fr? [R,L]
But this not :
RewriteEngine On
RewriteRule ^sandbox/video\.php?$ http://www.google.fr? [R,L]
I run Apache 2.2 and PHP-FPM inside a VM on Debian
Could it be related ?
Thanks for your help
With PHP-FPM the .htaccess file is never read for php URL. You will need to add the rewrites to your main config.
I'm trying to migrate a website from cPanel based hosting to AWS EC2 and am having problems with URL Paths being interpreted as sub-directories.
for example www.mysite.com/page/page-name is giving the error in the apache error log /var/www/html/page/page-name not found.
I've hardly changed the Apache config file at all.
As you can see above the site is running in /var/www/http, I'm not using any vhosts
The site is written using the Yii framework on PHP
The home page display OK, so php and Yii are OK.
I have the normal htaccess file in the web root:-
RewriteEngine on
AddDefaultCharset utf-8
DirectorySlash Off
RewriteBase /
# 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
and in Yii configuration I'm using the usual urlManager config:
'urlFormat' => 'path',
'showScriptName' => false,
Is there something I should be adding to httpd.conf?
You may need to enable AllowOverrides in order for your .htaccess to take effect.
Look in your httpd.conf file and find the following line
AllowOverride None
Change this to
AllowOverride All
There may be more than one AllowOverride directive. The one you want to change is the one within the Directory stanza for /var/www/html.
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
I am working with Yii Framework on Apache/2.2.15 (CentOS) Server.
Following line is uncommented in /etc/httpd/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
I can see mod_rewrite under Loaded Module when I do following
<?php phpinfo(); ?>
Yii Project Structure:
/var/www/test/
/var/www/test/index.php
/var/www/test/.htaccess
.htaccess content
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
It works fine when I do:
http://10.20.30.40/test/index.php/testcontroller/testaction
But when I do:
http://10.20.30.40/test/testcontroller/testaction
It shows following error:
Not Found
The requested URL /var/www/test/index.php was not found on this server.
Any idea?
I have the same trouble.
I use Apache config alias, like:
<VirtualHost *:80>
...
Alias /project "/Users/foo/Sites/project"
...
</VirtualHost>
To solve, I use "RewriteBase" directive on .htaccess, example:
RewriteEngine on
RewriteBase /project # <---- Modify here! and remove this comment.
# 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
Source: http://www.yiiframework.com/wiki/214/url-hide-index-php/#c9725
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
htaccess content.
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'),
urlmanager config in your main.php. Or you can customize it if you want.
Ensure that AllowOverride is "All" for your project's web directory in httpd.conf. If not, change the value and restart the web server
I solved this editing the Apache Configuration file at:
{{ApacheDir}}/conf/httpd.conf
And Un-commenting / Adding the following line:
LoadModule rewrite_module modules/mod_rewrite.so