2

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>
Community
  • 1
  • 1
benbob
  • 55
  • 1
  • 1
  • 7

1 Answers1

1

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

Rijndael
  • 3,253
  • 2
  • 22
  • 26
  • I will be using a shared hosting environment, so I'm not sure if I will have access to any apache config files. Right now I have a virtual host set up in vhosts, and in my httpd.conf I have will AllowOverride All. I didn't see in the Alias link where I set these alias' up as well. Thanks for the help – benbob Aug 23 '13 at 17:41
  • you can put the alias links in the htaccess file. Since you are using shared hosting there is no work around and you'll have to use htaccess. Just make sure mod_alias is loaded. – Rijndael Aug 23 '13 at 18:19
  • I also get a server error '500' when adding any Alias rules to the .htaccess file. LoadModule mod_alias.so is loaded in httpd.conf too. I also tried removing RewriteEngine on in vhost and httpd files. – benbob Aug 23 '13 at 18:24
  • Should I have my virtual host directory in the httpd file? When looking at the apache error log, I see multiple of these errors: "site_directory/.htaccess: Alias not allowed here" – benbob Aug 24 '13 at 00:04
  • Yes, it should be in either the server config or virtual host. – Rijndael Aug 24 '13 at 00:26
  • I've had it set up in the vhost file and my htaccess file is working because I have a file not found rule that works. Why else would the alias module not work? Nothing has really been solved here.. – benbob Aug 25 '13 at 20:09
  • Server config and virtual host does not include htaccess. See following link for an exposition of the mentioned terms: https://httpd.apache.org/docs/2.2/mod/directive-dict.html#ContextUnfortunately, if you are using htaccess then you cannot use mod_alias. – Rijndael Aug 25 '13 at 22:09