简体   繁体   中英

htaccess url-rewriting not working with config setup

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM