I've tryed to follow Yii2 Official Guide to configure Apache-2.4x64 for enablePrettyUrl option. My issue is that I've got 500 Server Error when trying to access localhost/about/web/ in my browser. I've placed .htaccess in my about/web folder.
Here's it's configuration:
# Set document root to be "basic/web"
DocumentRoot "d:/openserver/domains/localhost/about/web"
<Directory "d:/openserver/domains/localhost/about/web">
# use mod_rewrite for pretty URL support
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
# ...other settings...
</Directory>
Maybe something wrong with path to about/web? I have this error only on my Windows machine.
Directory and DocumentRoot are not allowed in the .htaccess file, they should be in the apache configuration (or in your VirtualHost config file).
In your case, the .htaccess file is being read, that is why it gives youa error 500 (the file is read but it containt systax errors or bad commands). Therefore, it should be enough to remove the 'DirectoryandDocumentRoot` commands and leave the file just with:
# use mod_rewrite for pretty URL support
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
Please, make sure that mod_rewrite is enabled in your apache installation.
Related
I have a site developed in Yii2 on a VPS (Apache 2.4.23) structured like this:
user/app (all the sites files and directories)
user/public_html
In public_html there are links to directories and files such as:
.htaccess (file folder)
admin (file folder)
assets (file folder)
css (file folder)
images (file folder)
index (php file)
The psite has a frontend and backend, the backend is on a subdomain, and frontend on the domain (example.com and admin.example.com)
Frontend load fine both with http and https, whereas the backend does not load the assets (css and images), https on backend redirects to http which I don't want to.
On the error logs I see a bunch (too many to list them all) of errors, like the following samples:
[access_compat:error][pid][client]AH01797:client denied by server
configuration: home/user/app/backend/web/index.php, referer
http://admin.example.com/en/user/index
[access_compat:error][pid][client]AH01797:client denied by server
configuration: home/user/app/backend/web/index.php
[access_compat:error][pid][client]AH01797:client denied by server
configuration: home/user/app/backend/web/en
The htaccess files are the same for frontend and backend like this:
# use mod_rewrite for pretty URL support
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 [L]
# use index.php as index file
DirectoryIndex index.php
I also tried (suggested by hosting support) with no success.
# use mod_rewrite for pretty URL support
RewriteEngine On
# enable symbolic links
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php [L]
# use index.php as index file
DirectoryIndex index.php
Could you please tell me what's wrong and why frontend works fine, but the backend does not?
In a cpanel setup, subdomains can only be served from directories under public_html.
I am trying to tidy up my site URL by removing the index.php entry script. There's a documented method to do this, found here: http://www.yiiframework.com/doc/guide/1.1/en/topics.url
The instructions basically to say to set 'showScriptName' to false within the Yii project config file.
They also say to enable the rewrite module within your Apache configuration. I manually enabled it within the httpd file for WAMP. See here. I could have also done it via the system tray interface.
Once that was saved, I restarted by server.
Lastly, (and this is what I'm the least sure about, but I copied it from instructions) I added some lines to the .htaccess file within the protected folder of my Yii Project.
AllowOverride all
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
Despite doing this, I am getting Not Found errors for every single one of my pages. I am unsure as to what has gone on for this to happen, or what I haven't done to enable it properly.
Thanks.
That .htaccess content goes in the .htaccess file in the root directory not the protected folder. It should then work. From http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x read this:
We can create the file /wwwroot/blog/.htaccess with the following content.
The following settings are what I use for every Yii project. Give that a shot.
.htaccess:
<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Don't perform redirects for files and directories that exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For everything else, redirect to index.php:
RewriteRule ^(.*)$ index.php/$1
</ifModule>
/protected/config/main.php
'urlManager'=>array(
'showScriptName'=>false,
I have an .htaccess file in my directory where I'd like to change about-us.php to /about-us/ and I have used mod_mod_rewrite generator to do so however either I get 500 internal server errors or it doesn't work. So I checked my apache and its already enabled as default - I also have confirmed this is enabled using
/usr/local/apache/bin/httpd -l | grep rewrite
and I get
mod_rewrite.c
but this is it, my server is centos 6
I really don't know what else I need to do, do you guys have any idea? code below is another try but even this doesn't work fine
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
Many thanks in advance
http://httpd.apache.org:
In general, .htaccess files use the same syntax as the main
configuration files. What you can put in these files is determined by
the AllowOverride directive. This directive specifies, in categories,
what directives will be honored if they are found in a .htaccess file.
If a directive is permitted in a .htaccess file, the documentation for
that directive will contain an Override section, specifying what value
must be in AllowOverride in order for that directive to be permitted.
http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride
<Directory /var/www/>
AllowOverride All
</Directory>
As #ava points out, make sure your Apache server is set to allow directives at the directory level via htaccess. It appears that your rewrite rule is backwards. You state that you want to redirect from files (about-us.php) to a directories (about-us) but your rule is set up for the opposite.
If you're just rewriting one file, do so explictly.
Rewrite On
# Rewrite specific file (not case sensitive, stop processing rules).
RewriteRule about-us.php about-us [NC,L]
Or, you can remove .php from all files. The following reverses the answer given here.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*).php $1 [NC,L]
Also consider where the URLs are in relation to the web root and where your htaccess file is (see RewriteBase). If this is a permanent redirect, you should let bots know with [R=301].
I have three web servers on three separate computers. I am trying to install a php application that rewrites all url requests to index.php (front controller pattern). The following .htaccess works on two out of the three web servers:
RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ index.php [NC,L]
So, when I visit http://example.com/hithere, index.php is called and I can then parse the originally requested url (hithere) to figure out what controller to send it to.
However, for some unknown reason, the above .htaccess file doesn't work on the third webserver. Instead, when I access http://example.com/hithere, I get a 404 not found error in my browser that says this:
The requested URL /full/path/to/application/index.php was not found on this server.
So, based on the above error, I can tell that it's actually trying to redirect to index.php. But the file that the browser lists in the /full/path/... below is actually on my web server - so I have no idea why it can't find it. Could someone please help me and tell me what I'm doing wrong?
Edit: I don't know if something in the httpd.conf could be conflicting, but here are the settings I have in httpd.conf for the directory that this application is in
Alias /myapp /full/path/to/my/app
<Directory /full/path/to/my/app>
Order allow,deny
AllowOverride All
Allow from all
</Directory>
Edit 2: I noticed something peculiar in the Apache logs. It looks Apache is appending index.php, which I am trying to redirect traffic to, to the DocumentRoot:
File does not exist: /document/root/full/path/to/my/app/index.php
When it should be going to:
/full/path/to/my/app/index.php
So I basically need to tell htaccess to ignore the document root. I tried this by specifying this in my .htaccess file:
RewriteBase /full/path/to/my/app/
But it's still searching the document root for index.php :/ Is there a way to reset the DocumentRoot or tell htaccess to ignore it when rewriting to index.php? Even when I use the absolute path in the RewriteRule, it still appends it to the document root.
RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ /full/path/to/my/app/index.php [NC,L]
It doesn't make sense to me... Can someone please help?
Thanks
So, based on the above error, I can tell that it's actually trying to
redirect to index.php. But the file that the browser lists in the
/full/path/... below is actually on my web server - so I have no idea
why it can't find it.
Assuming it's a *nix alike system, you might want to check file permissions and make sure that the webserver has read access to the file.
You also need to check the DocumentRoot parameter in httpd.conf
Look for something like this in the file and make sure it's correct. That is also where apache will be getting it from.
DocumentRoot "/var/www/html/path/to/whatever"
Also I would disable SELinux if it's on and you really don't need it because it causes weird problems too.
First enable rewrite module in apache by using $ sudo a2enmod rewrite then restart web server by $ sudo systemctl restart apache2
Put following code in your virtual host.
<Directory /var/www/html/your_app_folder>
Order allow,deny
AllowOverride All
Allow from all
</Directory>
then add following code in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I am working with a custom MVC PHP framework and the index page (acting as a router) receives a GET variable "do" which contains the path that it will route to. If this variable is not set, it defaults to the Auth controller, method login.
require_once('config.php');
$controllerAction = isset($_GET['do'])?$_GET['do']:"auth/login";
require_once('core/main.php');
Then the index page (source code above) passes this $controllerAction to the main.php file, which autoloads the main controller and then loads the requested controller.
Thus, the URIs in this framework are of the form mysite.com/?do=controller/method/variable and I need it to be in the form mysite.com/controller/method/variable.
Here is the .htaccess file I tried to use, it just didn't work (I have other htaccess files working on the same server so it's not an Apache problem) :(
RewriteEngine On
RewriteRule ^([^/]*)$ /?do=$1 [L]
Someone suggested that I can do this using PHP but I am not sure how to go about that.
Edit:
The error is that I get "This page cannot be displayed", 404 errors, whenever I try to directly access the mysite.com/controller/method links rather than the default mysite.com?do=controller/method
Further Edit
(please note that other virtual hosts work fine on my localhost):
(XAMPP) Apache Virtual Hosting Info:
<VirtualHost *:80>
DocumentRoot "D:\sites\mysite.com\root\wwwroot"
ServerName mysite.com
ServerAlias mysite.com
<Directory "D:\sites\mysite.com\root\wwwroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
File structure (Windows):
D:\
--sites
----mysite.com
--------#client_details
--------root
-----------#devfiles
-----------#vars_pwd
-----------wwwroot
--------------config
--------------core
--------------application
------------------controllers
------------------libraries
------------------models
------------------views
----------------------css
----------------------javascript
----------------------images
----------------------icons
First of all, there are some issues with your .htaccess contents. It's always a good idea to not rewrite if a file with the requested name exists. This allows you to have an img/ folder for your images or any other static content like css files, javascript, downloads, etc.. The first RewriteCond tells Apache to only rewrite if no folder with this name exists. The second one does the same with files. Then you probably want the QSA (i.e. Query String Append) option, which will pass all other GET variables to your script.
Under this conditions you can simplify the regex and use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L,QSA]
You might be surprised because this is more or less the same as others posted. I use similar things for many of my projects and I've just tested it, I can guarantee that it works. There must be something wrong with your apache config.
When you have problems with mod_rewrite, the first thing you should try is to enable the module itself. Type these commands as root in your shell:
a2enmod rewrite
/etc/init.d/apache2 restart
The first one activates the module (or complains with Module rewrite already enabled if everything is ok) and the second one restarts your Apache server. The path may of course be different on your server.
Then you have to make sure that your VHost config allows you to use .htaccess files and do rewrites. This means AllowOverride must be set to at least FileInfo (or All). You could also try to put the rewrite rules right into the config file. Your config should look similar to this:
<VirtualHost *:*>
ServerName test.example.com
ServerAlias www.test.example.com
DocumentRoot /home/sites/test/
<Directory "/home/sites/test/">
Allow from all
AllowOverride All
Options +Indexes
</Directory>
</VirtualHost>
Note that you have to restart Apache if you change anything in there.
If that all doesn't help, it's always a good idea to have a look at the error logs. On my system they're located at /var/log/apache2/error.log (debian). They might give you more information on what's going wrong.
Try
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?do=$1 [L]
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L]
Check your apache logs, access logs specifically. If the folder is present in the web root, then you should be able to access it directly :). You might also want to check if you have duplicate virtualhost entries for the same site by chance.
This one is my customized MVC framework which is based on cake
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?do=$1 [QSA,L]
</IfModule>
May be this should help. The typical URL pattern for this site.com/controller/method
I don't know what your domain setup is like, but here are some suggestions.
If your code resides in the root of your folder, and the index file is called index.php try the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?do=$1 [L,QSA]
If your website exists in a subfolder e.g. www.example.com/site/, and the index file is index.php Then try the following (change /site/ to whatever your folder is).
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?do=$1 [L,QSA]
If you still get the 404 error message then do the following:
Make sure your site allows .htaccess files to be processed by checking AllowOverride is set to all. If you don't have access to the necessary config files to check, a simple test is to setup an .htaccess rule to redirect to a dummy file on your system. If it works, then your .htaccess is being executed fine.
Have a look at your MVC framework to see what page it's actually sending the request to. The problem may be that you haven't defined a handler for that particular request, and the default action of your MVC framework is to throw a 404 error.
Edit: Just reading your description, I notice you said that the URL should basically be something like mysite.com/?do=controller/method/variable. If it has be very strict about this format, then you'll also need to put in rules for removing any leading or trailing slashes, e.g. the following re-write rule should do it:
RewriteRule ^\?(.*)\?$ /index.php?do=$1 [L,QSA]
(This makes the leading and trailing slashes optional, but it should remove them from the actual value you pass to do).