I'm on a win8 machine running XAMPP. I have a virtual host directory set up with a .htaccess file. I have been researching how to rewrite urls and I found the RewriteEngine module. In my httpd.conf apache file, the module was already enabled:
LoadModule rewrite_module modules/mod_rewrite.so
It seems like the next step was to update my .htaccess file like so:
php_value register_globals off
DirectoryIndex default.php index.php
ErrorDocument 404 /filenotfound.html
RewriteEngine On
RewriteBase /
RewriteRule ^Home$ Default.php [L]
RewriteRule ^AboutMe$ About.php [L]
RewriteRule ^Work$ Work.php [L]
RewriteRule ^Blog//Categories$ Blog/Categories.php [L]
RewriteRule ^Blog//([^/.]+)/?$ Blog/Posts.php?val=$1 [L]
I have followed a couple SO questions (config and rewriting w/ params), but am unable to get even the easiest rewrites to work. I have restarted apache a couple of times with no results.
While I'm here, this is my folder structure boiled down to everything relevant:
root
.htaccess
Blog
AuthorPanel.php
Categories.php
Post.php
Posts.php
Default.php
About.php
Work.php
And these are the rewrites I am looking to achieve (I have already tried most of them):
site.com/Default.php => site.com/Home
site.com/About.php => site.com/AboutMe
site.com/Blog/Categories.php => site.com/Blog/Categories
site.com/Blog/Posts.php?id=3&val=Android => site.com/Blog/Android
site.com//Blog/Post.php?id=4&title=Working+with+ActionBar => site.com/Blog/Working-with-ActionBar
Update 1 In httpd-vhosts.conf I even tried using the RewriteEngine on and rewrite rules, with no luck either:
<VirtualHost *>
DocumentRoot "C:/Users/ben/Documents/PHP/benWIT"
ServerName benWIT.local
<Directory "C:/Users/ben/Documents/PHP/benWIT">
RewriteEngine on
Order allow,deny
AllowOverride all
Allow from all
Require all granted
RewriteRule ^Home$ Default.php [L]
RewriteRule ^AboutMe$ About.php [L]
RewriteRule ^Work$ Work.php [L]
</Directory>
</VirtualHost>
Since you are using htaccess then you will need to make sure AllowOverride is set to All in your httpd.conf file:
AllowOverride All
This will allow you to use htaccess files. Having said that, as a general rule you don't want to use htaccess files or enable AllowOverride if you have access to the apache config files simply because it will use more resources to search the directory and find the htaccess files etc. Placing the changes into the httpd.conf file or conf.d/example_host.conf is much better.
One other note, mod_rewrite is over used and is really overkill for most purposes. I would advice you use mod_alias (see http://httpd.apache.org/docs/2.2/mod/mod_alias.html) instead. I should point out this can only be use in server configs or virtual hosts, so it will not work in a htaccess file. But it should be given preference should you have the choice between the two.
Alias /home /default.php
Alias /aboutme /about.php
Alias /work /work.php
AliasMatch /blog//([^/.]+)/? /blog/posts.php?val=$1
.. and so on.
Here is a good read on when not to use mod_rewrite:
http://httpd.apache.org/docs/2.2/rewrite/avoid.html
Related
I have two problems actually:
First, I’m trying to redirect several short URLs to a single page with more actions, like this:
RewriteEngine On
RewriteRule ^/login?$ ^login.php?action=login&next=$1 [L]
RewriteRule ^/reset?$ ^login.php?action=reset&next=$1 [L]
This is being written in the .conf file inside <Directory>. The problem is that the first rule gets executed, while the second doesn’t and I can’t figure why.
I also tried writing them like this:
RewriteCond %{REQUEST_URI} /login$
RewriteRule ^login.php?action=login&next=$1 [L]
RewriteCond %{REQUEST_URI} /reset$
RewriteRule ^login.php?action=reset&next=$1 [L]
I should probably mention that login.php does not reside in the root directory, but in different subdirectories.
What am I doing wrong and how can I fix it?
The second issue I have is that if I put an .htaccess file inside the root directory, the rules in the .conf file don’t get executed anymore.
Inside the .conf file I have these rules:
<Directory>
Options Indexes FollowSymLinks ExecCGI Includes MultiViews
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
</Directory>
Why is this and how can I fix it?
Just to extend on from my comments above. Place these rules in your site root .htaccess or in httpd.conf file:
Options -MultiViews
RewriteEngine On
RewriteRule ^/?login(?:/(.*))?$ subdir1/login.php?action=login&next=$1 [L,NC,QSA]
RewriteRule ^/?reset(?:/(.*))?$ subdir1/login.php?action=reset&next=$1 [L,NC,QSA]
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
Testing two identical folders , one placed in a Drupal install folder and the other in a plain html subfolder in
home root , only the php links for the one in CMS install work. This makes sense because Drupal has a settings php
file and .htaccess file which makes sure everything is in its proper place for links to work whether in home root or not. However I thought it would be easy enough to get the links to work in plain html subfolder with a simple rewrite
rule in the .htaccess file which exists in the php folder. Yet try as I might, nothing has worked so far.
The configuration in /etc/httpd/conf.d/my.conf
<VirtualHost *:80>
ServerName drupalsite.com
ServerAlias www.drupalsite.com
ServerAdmin vps#drupalsite.com
DocumentRoot "/var/www/html/drupalsite.com"
<Directory /var/www/html/drupalsite.com>
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName plainhtml.com
ServerAlias www.plainhtml.com
ServerAdmin vps#plainhtml.com
VirtualDocumentRoot "/var/www/html/plain”
</VirtualHost>
The .htaccess file in identical php folders
RewriteEngine On
RewriteBase /phpfolder/
RewriteRule ^c-(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
So drupalsite.com/phpfolder links all work
But plainhtml.com/phpfolder links do not work
phpfolder for Drupal resides in /var/www/html and Drupal site is symlinked
phpfolder for plain html subfolder to root resides in /var/www/html/plain
Both phpfolders are owned by apache:root and all files within are owned by root:root
Have also tried changing the .htaccess rewrite rule to:
RewriteBase /
RewriteBase /plain/
RewriteBase /plain/phpfolder/
You will have to define a Directory entry with AllowOverride for:
/var/www/html/plain
<Directory /var/www/html/plain>
AllowOverrideAll
Allow from all
</Directory>
Note: If you have access to the main configuration file of the server you shouldn't be using .htaccess, configuring Rewrites or Redirects in Virtualhost is simpler. As you have already seen, using sub-files and per-dir configurations make things more complicated, not counting the overhead of apache having to constantly check those files when AllowOverride is enabled.
I want to do routing in code-igniter and i have done the below steps also
but it is not working.I have done all the required settings also but then also
its not working.Please let me know is i am missing any thing or not.
Step 1
Add This in .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Step 2:
Remove index.php in codeigniter config
$config['index_page'] = '';
Step 3
Add this in route.php
$route['Viewsignup'] = 'Login/index/Viewsignup';
My url is like that:
http://localhost/machinetest/index.php/Login/Viewsignup
I want this url:
http://localhost/machinetest/index.php/Viewsignup
Please help me,
Thanks in Advance
.htaccess should not be issue here. Try this
$route['Viewsignup'] = 'Login/Viewsignup';
This will take you into Login controller's Viewsignup method when your URL looks like http://localhost/machinetest/index.php/Viewsignup
And for removing index.php from URL just update your .htaccess with following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now http://localhost/machinetest/Viewsignup URL will work too
On some servers, Apache is configured to ignore some or all directives in .htaccess files. This is for security reasons. The AllowOverride directive controls which features will be allowed in .htaccess files. For example AllowOverride None can turn off htaccess files for a folder and its subfolders.
Check your Apache configuration file for which AllowOverride directive is applied to the directory containing your problem htaccess file.
If you’re not sure which configuration file to look in, start with the main Apache configuration file httpd.conf or apache2.conf. If your website is configured in a file included by httpd.conf (e.g. a virtual hosts configuration file), you will need to look in that file. See Location of httpd.conf on CentOS, Ubuntu, Mac and others to locate your httpd.conf.
To enable using a .htaccess file, change AllowOverride None to AllowOverride All.
For example, for a CentOS 5.3 server, I needed to change the AllowOverride setting in the file /etc/httpd/conf.d/virtualhosts.conf.
httpd.conf before:
Options FollowSymLinks
AllowOverride None
httpd.conf after:
Options FollowSymLinks
AllowOverride All
Be aware that enabling htaccess files has security implications, as htaccess files override your Apache configuration. For example, if your site provides uploads, a hacker could potentially upload a .htaccess file to your server and use it to gain access to your server. There are options to AllowOverride that restrict the directives that will be used from a .htaccess file. See the documentation for AllowOverride.
refer http://smartwebdeveloper.com/apache/htaccess-problems
I have a problem of too many redirection in one cakephp site.
Here is the site: http://pexinxas24.com/
This domain is pointed to root of the site and not to app folder.
Here is the code which is placed in htaccess of the root folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Here is the code which is places in the htaccess of the app folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
I am not getting any typs of PHP or cakephp errors.
This site always redirect to the root of the site. Whole code was working properly on old server. I have just changed the server and getting this error.
Looks like your are using Amazon-EC2 right? ;)
Well, probably you aren't redirected for the next reasons:
mod_rewrite it isn't enabled: Look inside of your httpd.conf, you should have the next line uncommented:
LoadModule rewrite_module modules/mod_rewrite.so
You haven't enabled override in htaccess: Look inside of your http.conf for the
directive "Directory", inside you should have something like this:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
The most important line of the previous code is AllowOverride All, on amazon instances is set to AllowOverride none by default, so you should change it to All, after that you must restart your apache server to reflect changes.
Hope it helps.
This is an old thread.
Probably if you understand about apache2 Ismael's answer is quite enough, but if you're a newbe in this subject as I am, might be useful saying that in apache2 nowadays you should "replace" the part one where he says
mod_rewrite it isn't enabled: Look inside of your httpd.conf, you should have the next line uncommented: LoadModule rewrite_module modules/mod_rewrite.so
for copying rewrite.load from mods-available to mods-enabled:
cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
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]