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.
Related
I can't seem to get my head around this. I just pulled down my website files from my server to work on them offline. However, my rewrite conditions are no longer working as expected.
I've been googleing for the last 3 hours and keep coming to these solutions:
put garbage in the .htaccess file to make sure it's being read. I did that and got a 500 error so it is.
Make sure mod_rewrite is enabled, and I make sure it was listed in php_info(). That's not the problem.
Other than that, I can't figure this thing out.
Here's my .htaccess. All I want to do is remove index.php from my URLs:
# Rewrite url no index.php
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule . index.php
Here's my virtual host config at the moment:
<VirtualHost *:80>
ServerAdmin xxxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I'd also like to make it known that rewrite rules seem to work a bit when they're in the virtual host config. So something like this:
<VirtualHost *:80>
ServerAdmin xxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I had other problems with this though since I have nested applications (one's at / and the other at /app2/). I also seemed to have problems with the !-f condition being ignored and it would rewrite the URLs of my images and css.
Does anyone have any ideas on how to fix this? Thanks in advance!
You should use your code like this to remove index.php from the URL.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L,QSA]
And then in your head section of your html add this to fix CSS problem.
<base href="http://myapp.local" />
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]
I have an application hosted in Apache
http://www.example.com
The DocumentRoot is set as /var/www/html/myapp
Now, I want to write a rule, such that, if someone triggers the URL http://www.example.com/abc, it should redirect to http://www.example.com
The tricky part is, this redirection should happen only if user directly copy-paste http://www.example.com/abc or refresh the browser when the user is in http://www.example.com/abc
"abc" can be any string.
I got this working with the following addition to httpd.conf
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html/abc
<Directory /var/www/html/abc>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
I have module in the frontend, and I want to make a friendly url like:
//yourdomain/a-zA-Z_module-controller-action-id-page-1.html
such as like this link:
http://kudufood.com/com-van-phong-lam-the-nao-de-nau-canh-bong-cai-duoc-ngon-kudu_cookbooks_1429067720.html
You can enable pretty URLs by adding the following code to your application components in config/web.php and configure custom URLs in 'rules':
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName'=>true,
'rules' => [
// your rules go here
'customURL'=>'my-controller/my-action'
],
If you wish to hide index.php as well, then set:
'showScriptName'=>false
and create an .htaccess file your /web directory with the following contents:
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
Documentation: http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
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