0

I'm trying to use .htaccess file to change and clean URLs.
My test site is on xampp server and I configured this using virtualHost.

This is my Virtual Host setting:
httpd

Alias /MySite "C:/xampp/htdocs/MySite"
    <Directory "C:/xampp/htdocs/MySite">
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

vhost

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin myMail@gmail.com
    DocumentRoot "C:/xampp/htdocs/MySite"
    ServerName MySite
    ErrorLog "logs/MySiteErrorLog.log"
    CustomLog "logs/MySiteCustomLog.log" common
</VirtualHost>

When I use very simple commands in .htaccess file, there's no affect on URL but if I try some wrong code, I will get Error 500.

Would you help me to understand why is that?
Is there any thing wrong in my settings?

UPDATE :
It's my site structure :

MySiteRoot
  |
  |
   PHP //directory
      |
      Assets  //directory
      classes //directory
      pages   //directory
      parts   //directory
      .htaccess //file
      index.php //file


This a simple test in .htaccess file :

RewriteEngine On
RewriteRule ^([^/]*)/$ /mySite/PHP/index.php?page=$1 [L]




These images will describe better: using .htaccess file



without .htaccess file



In both situations the url is the same and there's not error or something

Thanks in Advance...

vahed
  • 161
  • 1
  • 2
  • 12
  • The basic config looks plausible. What is in the .htaccess file and what urls are you using to test whether it works? – Steve E. Sep 13 '15 at 20:10
  • @SteveE. Thanks for your reply. I did update the question. please take a look... – vahed Sep 13 '15 at 21:25

2 Answers2

0

You need to place your .htaccess in C:/xampp/htdocs/MySite, not in any sub-directory like PHP. You can use this rule in C:/xampp/htdocs/MySit/.htaccess:

RewriteEngine On

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ PHP/index.php?page=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for your answer. I did test it before, there's no difference when I place `.htaccess` file in root folder. And I changed my file location and code based on your suggestion and the problem still remained... – vahed Sep 14 '15 at 08:02
  • I don't get error. In fact there's not any affect just like when there's no htaccess file. When I type a wrong rule, I'll get error and it's pretty normal, but when I write a rule, there's no affect... :-| And YES, mod_rewrite is enabled... – vahed Sep 14 '15 at 11:06
  • I'll visit index.php file, this file is front controller, when I click on a link in this page, the link will make queryString based on link target and the url will be created... I did test both above URLs and there's `404` error ---> `object not found` !!! – vahed Sep 14 '15 at 11:25
  • No, I told that, url `do not` change and the browser will display the page correctly but with old URL, There's not error.... – vahed Sep 14 '15 at 11:42
  • Exactly, this is the point. but, I visit first one using `.htaccess` file and for creating the second, I did delete `.htaccess` from my site... As you can see these are the same and it seems htaccess do not affect at all. But when I type random text to `.htaccess` file, I'll get error 500 that shows htaccess is in use... – vahed Sep 14 '15 at 13:17
  • What is `/ketabTheme/` fodler in your URL? What happens when you visit this URL: `http://localhost/ketabTheme/home` in browser? – anubhava Sep 14 '15 at 15:31
  • `/ketabTheme/` is equal to `/mySite/` in my question. I changed it because mySite is understandable. when I try to visit `http://localhost/ketabTheme/home` , I'll get an 404 (Object not found) error... – vahed Sep 15 '15 at 08:34
  • ok move above .htaccess inside `/ketabTheme/` folder and retest `http://localhost/ketabTheme/home` URL – anubhava Sep 15 '15 at 08:48
0

I think there's an issue in your rewrite rule, it's only matching requests that end in a trailing slash. Assuming

  • The .htaccess file is in your web root
  • You are trying to match everything up to the first optional forwardslash and pass that into PHP

Then this should work:

RewriteEngine On
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+) PHP/index.php?page=$1 [L,QSA]

Adding this into your vhost (apache 2.4.x) will enable rewrite debugging

<VirtualHost *:80>
    ... existing config
    LogLevel alert rewrite:trace3 
</VirtualHost>

If it works then you will get output in the new log file explaining what Rewrite is doing

Steve E.
  • 9,003
  • 6
  • 39
  • 57
  • Thank you too, but again this code don't affect... I did test so many different rules... I named my question `strange behavior` because of this :) – vahed Sep 14 '15 at 11:00
  • Ok, Rewrite is not doing what you expect. Have you tried logging? I have updated the original answer to include these steps. – Steve E. Sep 14 '15 at 12:53
  • Yes, you're right. When I was added this commands to my vHost based on your answer, xampp will not start correctly and show me an error that says something is wrong. I'm using xamp 3.2.1. As much as I fiundout in some search like http://stackoverflow.com/questions/13937449/cant-configure-the-rewrite-log, I have to write a new command based on my version. would you please let me know what's wrong to start `RewriteLog` ? Thanks again... – vahed Sep 15 '15 at 08:30
  • I think you are using cpanel 3.2.1 as I cannot find a 3.2.1 version of XAMP. I have updated the answer above to have the logging configuration for apache 2.4.x. It will log to "logs/MySiteErrorLog.log" [http://httpd.apache.org/docs/current/mod/mod_rewrite.html] – Steve E. Sep 15 '15 at 10:28