0

working for rewrite rules for my website couples of days now but failed to do it successfully. googled a lot and also found some very important threads here in stackoverflow,specially this one. I have made all the changes which are mentioned in the web.Before i talk about the changes i made to my httpd.conf, i want share the directory structure of my wamp

root directory for my website is C:/wamp/www/dist. index.php and .htaccess files for my website are kept Within this dist folder and there are other sub-directories for other files for my website.

MY CHANGES ::

wamp --> Apache --> Apache modules --> rewrite_module checked it

       ----------------IN MY APACHE httpd.conf file----------------

 LoadModule rewrite_module modules/mod_rewrite.so //removed # sign from it

 <Directory />
      Options FollowSymLinks
      AllowOverride All
      Order allow,deny
      Allow from all
  </Directory>

  <Directory "c:/wamp/www/">
      Options Indexes FollowSymLinks
      AllowOverride all
      Order Allow,Deny
      Allow from all
  </Directory>

  <Directory "c:/wamp/www/dist/">
     Options Indexes FollowSymLinks MultiViews
     AllowOverride all
     Order allow,deny
     Allow from all
  </Directory>

and now i have created a .htaccess file in my C:/wamp/www/dist (index.php is also here)

so my .htaccess looks like

<IfModule mod_rewrite.c>
     Options +FollowSymLinks
     RewriteEngine On
     RewriteBase /dist/
     RewriteRule ^([^/]*)\.html$ /dist/?path=$1
</IfModule>

Result/Error :: It is not working url remains the same(ugly) as before.no changes in url.

Error Explained :: from my any pages which are in other sub-directories i send a request to my index.php which looks like http://localhost/dist/?path=home and after checking the value of path variable index.php includes necessary file.so I want to my url as http://localhost/dist/home.but as i said it is not working.my url still remains http://localhost/dist/?path=home after creating .htaccess file

Note 1 :: i checked phpinfo() the modules are loaded.

Note 2 :: I tried by removing <IfModule mod_rewrite.c> from .htaccess but it has no effect all are same again

Community
  • 1
  • 1
RbG
  • 3,181
  • 3
  • 32
  • 43

1 Answers1

1

Keep your /dist/.htaccess like this:

DirectorySlash Off
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /dist/

# Use %{THE_REQUEST} to make an external redirect from /dist/?path=home to /dist/home
RewriteCond %{THE_REQUEST} \?path=([^\s&]+)\s [NC]
RewriteRule ^ %1? [R=302,L,NE]

## internal rewrite from /dist/home to /dist/?path=home
# not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?path=$1 [L,QSA]
  • THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1
  • QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Reference: Apache mod_rewrite Introduction

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • can you please explain your codes sir.. i dont know/understand the codes..i am copying it and if it runs please let me know how did it work because i am new to `.htaccess` – RbG Jul 29 '14 at 16:32
  • sir voted it up...can accept it after few minutes...can you please explain the error and changes – RbG Jul 29 '14 at 16:34
  • I have added explanation. You can also refer http://askapache.com/ as a good reference on this. – anubhava Jul 29 '14 at 16:46
  • sir i have three queries... **1>** Should i write Line No. 4 & 5 only once for all my rules? ... **2>** should i repeat Line no. 6 & 7 for all my rules ... **3>** will it work for multiple url variables like if i want to rewrite `http://localhost/dist/?path=view_post&post_id=72&post_title=here goes_the_post_title` – RbG Jul 29 '14 at 16:47
  • Multiple parameters will need a new set of these 2 rules: 1. External redirect rule and 2. Internal rewrite rule. You will need to be careful using correct regex for that. Feel free to post a new question for more complex requirements since these rules cannot be posted in comments section. – anubhava Jul 29 '14 at 16:54
  • sir i faced a problem with your solution .... thye problem is it is working for the first time only..for example if i pass `http://localhost/dist/?path=home` for the first time then it is working fine now my url is `http://localhost/dist/home/` but if i send now `http://localhost/dist/?path=jobs_post` it is failing now it is showing `http://localhost/dist/home?path=jobs_post` instead of `http://localhost/dist/jobs_post/` – RbG Jul 29 '14 at 16:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58270/discussion-between-anubhava-and-ritabrata-gautam). – anubhava Jul 29 '14 at 16:58