How to remove index.php from Yii URLs in Apache/Centos? - apache

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

Related

multiple files renamed in directory, need htaccess rule to redirect

Is it possible in the .htaccess file to redirect multiple file names, for example I use to have files such as:
/publications/publications-1.php
/publications/publications-2.php
/publications/publications-3.php
I want to change them to their new file names:
/publications/1.php
/publications/2.php
/publications/3.php
Try it like this in directory where you have your php files,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+).php$ publications-$1.php [L]
Add a htaccess file to your webroot directory and add a rule like this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^publications/(.*)\.php$ /publications-$1.php [L,QSA]
</IfModule>
## Results
# publications/data.php => publications-data.php
It maps any request that matched the condition to the corresponding php file under the hood. If you need a redirect instead, append a R flag to the rule.
In case you need a more general rule to match anything in the first segment, try this instead:
RewriteRule ^(.*)/(.*)\.php$ /$1-$2.php [L,QSA]
## Results
# foo/bar.php => foo-bar.php
And one last thing to note; make sure apache's rewrite module is enabled and htaccess files are allowed (AllowOverride All in your httpd.conf):
# If enabled, outputs something like this:
# rewrite_module (shared)
apachectl -M | grep rewrite # It's apache2ctl on some platforms
# If not enabled; you can enable it using this on most platforms:
a2enmod rewrite
apachectl restart

backend urls broken in yii 2 advanced template

After enabling pretty urls in site and setting up apache virtualhost, navigation is broken on backend, returns #404 for all pages.
Here are configuration files:
httpd-vhosts.conf file:
...
<VirtualHost nvp.dev>
Alias /backend "d:/dev/htdocs/nvp/backend/web/"
DocumentRoot "d:/dev/htdocs/nvp/frontend/web/"
ServerName nvp.dev
ServerAlias www.nvp.dev
</VirtualHost>
.htaccess:
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
common\config\main-local.php
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
]
What am I missing here?
Fixed.
In case if someone has similar problem:
RewriteBase /backend
Needs to be added in .htaccess, located at backend/web directory.

Htaccess mod rewrite don't work

I have a project with two applications: a frontend (in AngularJs) and a backend (in Phalcon). I my server document root i have two folders and one htaccess:
public_html
- api
- controllers
- index.php
- app
- .htaccess
The .htaccess have the next configuration:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/(.*)$ api/index.php?_url=/$1 [QSA,L]
</IfModule>
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
So, when i send a POST request (for example) to http://mydomain/api/sessions, the result is 404 not found. The router configuration is:
$router->addPost('/sessions', array(
'controller' => 'sessions',
'action' => 'post'
));
In my localhost works fine with this configuration. But, in my VPS not.
Any ideas ?
Update 1:
If i don't use any REST service, accessing via http://mydomain/api the Phalcon index controller is loaded.
Update 2:
If i try to access a REST service using a Phalcon url like http://mydomain/api/index.php?_url=/licenses works fine.
i found the problem. the virtual host configuration file had not the next lines:
<Directory "/var/www/domain/public_html">
Options All
AllowOverride All
Allow from all
</Directory>
You can sometimes get away with relative path substitutions in the document root, however, you should make the RewriteRule substitution root-relative (starts with a slash), or specify a RewriteBase / directive after you enable the rewrite engine in order to explicitly specify the URL prefix.
RewriteRule ^api/(.*)$ api/index.php?_url=/$1 [QSA,L]
Becomes:
RewriteRule ^api/(.*)$ /api/index.php?_url=/$1 [QSA,L]

Laravel: 404 Not Found or 403 Forbidden

I tried for the first time the Laravel Framework on my VPS.
In a folder where I have a other project folder (without any access problem) I create a project with "laravel new Test"
I changed the permission on "storage" and "bootstrap/cache".
Now I go to the page and...
"/Test/" => returns 403 Forbid
"/Test/index.php" => returns 404 Not Found
The mod_rewrite is enabled.
I set "AllowOverdrive All" to the main folder.
the .htaccess in the Test/public is the default one and i tried to change it to:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Restart httpd. restart all the vps... nothing changed...
What I miss?
The default documentRoot for access the content is in the folder /public
So you can access with www.example.com/public/
You should change the default root for example.com to the public folder.

apache 2.4 module rewrite error

trying to generate dynamic Urls using Module rewrite , it seems like m doing something wrong and getting Server error! server encountered an internal error
i used this code
<IfModule mod_rewrite.c>
#Enable mod_Rewrite
RewriteEngine on
RewriteBase /project
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html?)\ HTTP
RewriteRule ^(.*)index\.(php|html?)$ $1 [R=301,L]
</ifModule>
the project is aliased to C:/project
any pointers to achieve what m trying to?? m using apche 2.4 should the .htacess be saved in project folder ?? or conf folder ??
i want to achieve something like this
localhost/project/index.php?departmentId=2/
localhost/project/science-d2/
both should load same page
You are using an infinite loop, like this:
Suppose that your apache is configured like this in some config file:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml
Your rewrite rule may result in:
/project/index.php => /project => /project/index.php => /project => ...
In this case, you've no need of rewrite. So the solution here is comment out all the instructions:
#<IfModule mod_rewrite.c>
# #Enable mod_Rewrite
# RewriteEngine on
# RewriteBase /project
# RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html?)\ HTTP
# RewriteRule ^(.*)index\.(php|html?)$ $1 [R=301,L]
#</ifModule>
Since you are using Apache 2.4 try to replace <IfModule mod_rewrite.c> with <IfModule rewrite_module>
So the final code looks like this:
<IfModule rewrite_module>
#Enable mod_Rewrite
RewriteEngine on
RewriteBase /project
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html?)\ HTTP
RewriteRule ^(.*)index\.(php|html?)$ $1 [R=301,L]
</ifModule>
Of course make sure you comment out rewrite_module in .conf file so it is indeed loaded.