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].
Related
I currently have a problem with rewriting my URL's using the .htaccess file when a Laravel project is in a subdirectory.
usually when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works.
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
The home page works correctly though any links just return a not found error.
What changes would I need to have the .htaccess file do this for me.
so on investigating further upon entering 'index.php' into the url itself the page loads correctly if this helps anymore with answering the question.
Thanks!
I wouldn't modify the default .htaccess provided in a Laravel app. You need to use Apache's directory alias in the virtual host config file for your site. You need to have the following format:
#Change the paths accordingly
Alias /vacancies /path/to/app/public
<Directory /path/to/app/public>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted #If using Apache 2.4 add this.
</Directory>
I managed to fix this thank you for your suggestions, This turned out to be a problem with the virtual host file and where the project was created as I have soft links let up to direct apache.
As such Apache was looking in the actual project location set in the virtual hosts file rather than the soft link one intended causing the not found issues.
Check of .ht* permissions in the apache httpd config file if you are using apache. I had the same problem and I had permissions denied problem for .htaccess file. If you are on shared host, try to contact hosting support.
Tried putting it on top of the htaccess file ? Helps for me in some kind of cases, seems weird but it does.
I notice fro your answer you've managed to "fix" this. However, you have some fundamental errors in the .htaccess file you posted, so I'm not sure how this is working exactly?
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
These two directives are not valid in .htaccess files and consequently will result in a 500 Internal Server Error. These can only be used directly in the server config or virtual host context. These are also Apache 2.2 directives, so won't work on Apache 2.4.
RewriteRule ^index.php [L]
(Part of the "front-controller"). You are missing a space between the first and second arguments, so this will fail to rewrite any requests to index.php - your "front-controller". (Maybe this is just a typo, but it's difficult to see a typo like this could creep in?) For example, this should be more like:
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
These directives should be before the front-controller, not at the end of the file. Otherwise, they are not going to execute for any request that is routed through your framework.
when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works
That's not actually what the above .htaccess code does. It should simply be rewriting the request to /index.php. (The frawework/Laravel then looks at the full URL that was requested.) If it rewrote the URL to /index.php/vacancies (that some frameworks do) then you would need to read the URL using pathname information (PATH_INFO).
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
See above regarding the PATH_INFO. But if the .htaccess file is located in the document root of the site and the /abc subdirectory should be entirely hidden then you still have some work to do. You'll need to set the appropriate RewriteBase and modify the condition that removes the trailing slash off non-directories.
Otherwise, if the /abc directory is part of the URL then the .htaccess file can be moved to the /abc subdirectory, but you'll still need to modify the directives that remove the trailing slash.
I'm setting my .htaccess file right now to use friendly urls (manually). But, when i go to the url the server shows me Error 404.
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
Rewriterule ^register$ register.php
I'm really sure that mod_rewrite is enabled because i see it when use phpinfo().
Even though mod-rewrite is enabled, by default it is not enabled for .htaccess files.
Hold Your Breath
Open xampp control panel
Stop Apache
Click the Config button on the Apache row, and select httpd.conf
In that file, search for something like xampp/htdocs">
A bit lower, you may see a line like this: # AllowOverride All. Remove the #, which is a comment
Alternately, search for AllowOverride All, make sure it is in the right section, and remove the comment #
Save the file
Restart Apache, say a prayer, cross your fingers and hold your breath
This listing may be old but is still relevant so to make it a little more secure it is probably best to add a few lines.
Deny from all
Allow from 127.0.0.1 ::1 localhost
Allow from .htaccess
Also as a side note .htaccess files slow the speed of the server so it would be best to refer to xampp documentation for best practices.
Hope it helps.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
if we want to remove index.php form url .First we create a .htaccess file on root directory of project with out any extension. And add this code in this file and so remove the index.php Application/config/config.php
Check in httpd.conf you tag, review this property
AllowOverride All
AllowOverride authorize use htaccess file.
So I have a server running on Digital Ocean. I'm trying to run Anchor CMS on it. So this is what the URL should look like http://colourity.com/posts/hello-world. But that doesn't seem to work. When I try http://colourity.com/index.php/posts/hello-world, all works fine. But the problem here is that I'm using .htaccess to correct this, here's what I have,
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
That should of gotten rid of the index.php, additionally mod_rewrite is enabled. I've also restarted the server to see if that makes a difference, but it hasn't. Any ideas?
AllowOverride All must be included in the Apache configuration so that .htaccess rules can overwrite your configuration. From the documentation:
When the server finds an .htaccess file (as specified by AccessFileName) it needs to know which directives declared in that file can override earlier configuration directives.
When this directive is set to None, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.
When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files.
It's default value is All, but some template configuration files may have it manually set as None. Also note, that this syntax is only available in <Directory /> sections.
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).
I have a website, lets say: http://www.example.com/
I'm using rewrite module, but I have a subfolder forum.example.com
I don't want the .htaccess to affect this directory, how do I do it?
If your forum.domain.com points to a subdirectory like you suggested (and your current .htaccess file is in the directory that contains that subdirectory, or some directory above it), then all you have to do is add a .htaccess file in the subdirectory with the following:
RewriteEngine Off
And it will disable mod_rewrite for any requests that point to that directory.
In httpd.conf:
<Directory /relevant/directory>
AllowOverride None
</Directory>
See the documentation.
It is probably easiest to precede the RewriteRule with a RewriteCond directive
RewriteCond %{HTTP_HOST} ^www.domain.com$
This way, the RewriteRule is only applied to requests matching the RewriteCond, which required the HTTP host header to match www.domain.com.
(assuming Apache 2.2; I'm not sure whether this syntax is different for previous versions).
Set AllowOverride directive in the subdomain virtualhost to None
You could use a RewriteCond to only apply your rule when a particular domain name is used.
RewriteCond %{HTTP_HOST} ^www\.domain\.comt$ [nocase]
RewriteRule foo bar [l]