Please help with RewriteEngine in htaccess file

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Please help with RewriteEngine in htaccess file

Postby Seymour Clufley » 22. January 2009 15:54

I'm using Xampp in an offline PC to develop my website. It's v1.70, with the patch installed.

In the httpd.conf file, the ReWriteModule line is uncommented.

Here's the contents of my htaccess file:
DirectoryIndex cover.html
AddType text/x-component .htc


RewriteEngine On

RewriteCond %{REQUEST_URI} ^/sub/
RewriteRule /sub/(.*)$ sub2 [QSA,L]
The first two lines are for something else and they work as desired. But the rest don't!

This is my first attempt at Mod_Rewrite so the goal is simple: to change a URL like this:
http://www.mysite.com/sub/test.html
to this:
http://www.mysite.com/sub2/test.html
Can anyone tell me what I'm doing wrong?

Thanks,
Seymour.
Seymour Clufley
 
Posts: 11
Joined: 16. March 2008 09:40

Re: Please help with RewriteEngine in htaccess file

Postby LazerDino » 22. January 2009 17:23

Have you checked that the mod_rewrite is actually loading? You can verify by adding the following code to a plain text file and naming it phpinfo.php and upload it to your server. Then, call phpinfo.php and it will list loaded modules.

Code: Select all
<?php phpinfo(); ?>
LazerDino
 
Posts: 5
Joined: 30. December 2008 19:36

Re: Please help with RewriteEngine in htaccess file

Postby Izzy » 22. January 2009 22:27

Seymour Clufley wrote:In the httpd.conf file, the ReWriteModule line is uncommented.

Check out the xampp\apache\logs\error.log file because if there are issues with the .htaccess you should be receiving a server 500 error.

What error message are you receiving?


Some help with rewrite here:
I wrote a Beginners Guide - mod_rewrite and RegEx on the forum here a while back:
viewtopic.php?f=16&t=20957

http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html
http://corz.org/serv/tricks/htaccess2.php?page=3
http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html
http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/
http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html

Possibly solution but experiment with the base path also Options are usually set in the httpd.conf file for the htdocs folder by default but if your site is located outside the htdocs folder or on another server then include the Options but if you include it anyway it will do no harm nor will it prevent the .htaccess from running.:

Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteBase /   #<--experiment with the base path as in XAMPP / = htdocs
RewriteCond %{REQUEST_URI} ^/sub/
RewriteRule ^/sub/(.*)$ sub2 [QSA,L]
Possibly missing a ^ start of a string
Last edited by Izzy on 22. January 2009 23:02, edited 2 times in total.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Please help with RewriteEngine in htaccess file

Postby Wiedmann » 22. January 2009 22:35

Code: Select all
RewriteRule /sub/(.*)$ sub2 [QSA,L]

The RewriteRule pattern can't start with a slash inside a ".htaccess".
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: Please help with RewriteEngine in htaccess file

Postby Seymour Clufley » 23. January 2009 00:03

I did the phpinfo test that LazerDino suggested, and mod_rewrite showed up in the list of loaded modules.

The error I'm getting is #500.

I've checked the error.log file and it says "File does not exist" and then the unaltered URL! It's as if the rewriting isn't happening at all!
Seymour Clufley
 
Posts: 11
Joined: 16. March 2008 09:40

Re: Please help with RewriteEngine in htaccess file

Postby Seymour Clufley » 23. January 2009 01:27

The above has been updated.
Seymour Clufley
 
Posts: 11
Joined: 16. March 2008 09:40

Re: Please help with RewriteEngine in htaccess file

Postby Izzy » 23. January 2009 01:34

Seymour Clufley wrote:I did the phpinfo test that LazerDino suggested, and mod_rewrite showed up in the list of loaded modules.

The error I'm getting is #500.

I've checked the error.log file and it says "File does not exist" and then the unaltered URL! It's as if the rewriting isn't happening at all!
File does not exist is a 404 error and a 500 server error is different.

Can you paste the relevant parts of the error.log file please.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Please help with RewriteEngine in htaccess file

Postby Seymour Clufley » 23. January 2009 17:07

Sorry, you're right. The HTML page showing is 404. Error 500 came when I did something else.

To recap, I'm using Xampp on an offline PC, testing a site stored in "P:\website\upload\". That's the root directory. It is set up in httpd.conf as an alias, called "slush".

Now, when I open the page "http://localhost/slush/sub/test.html", I get a 404 page and this in the error log:

Here's the line from the error log:
[Fri Jan 23 15:15:41 2009] [error] [client 127.0.0.1] File does not exist: P:/website/upload/sub/test.html


The URL is supposed to get rewritten to "http://localhost/slush/sub2/test.html". The sub2 directory exists, as does the test.html inside it. (There is no "sub" directory, only "sub2".)

Here's the htaccess file:
DirectoryIndex cover.html
AddType text/x-component .htc
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^/sub/
RewriteRule ^/sub/(.*)$ sub2 [QSA,L]


The same result happens whether I un/comment the lines starting with DirectoryIndex, AddType and RewriteBase.

Would it be helpful if I pasted the entire httpd.conf file?
Seymour Clufley
 
Posts: 11
Joined: 16. March 2008 09:40

Re: Please help with RewriteEngine in htaccess file

Postby Izzy » 24. January 2009 01:34

Seymour Clufley wrote:To recap, I'm using Xampp on an offline PC, testing a site stored in "P:\website\upload\". That's the root directory. It is set up in httpd.conf as an alias, called "slush".
Please paste this Alias Directive including several lines above and below from the httpd.conf file.

Ideally the Alias Directive would reside in the .\apache\conf\extra\httpd-xampp.conf file with all the others of which there are several working examples - if your Alias is syntactically correct then try and move it into this file then restart Apache and this restart would be required after each edit session with any Apache conf file but not necessary when editing the .htaccess file.

Not sure yet if there is a need for a redirect when using an Alias Directive as that is the intended use of an Alias any way.
Code: Select all
    Alias /slush/sub "P:/website/upload/slush/sub2"
    <Directory "P:/website/upload/slush/sub2">
        AllowOverride AuthConfig
        Order allow,deny
        Allow from all
    </Directory>
Just tested this and it worked as you intended using http://localhost/slush/sub/test.html

You can instead use AliasMatch Directive with regex if you prefer.


Seymour Clufley wrote:Now, when I open the page "http://localhost/slush/sub/test.html", I get a 404 page and this in the error log:

Here's the line from the error log:
Fri Jan 23 15:15:41 2009] [error] [client 127.0.0.1] File does not exist: P:/website/upload/sub/test.html
The missing slush directory is added when the RewriteBase is correct /slush see below but the .htaccess will still give a 404 error because the rule is not quite right.

Seymour Clufley wrote:Here's the htaccess file:
DirectoryIndex cover.html
AddType text/x-component .htc
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^/sub/
RewriteRule ^/sub/(.*)$ sub2 [QSA,L]
I adjusted this and still a 404 error so not sure what is wrong as I ran out of time testing the rule - the index works and the addtype is irrelevant to the rewrite rule, the RewriteBase is correct now and eliminates the need to add /slush into the rules /slush/sub/:
Code: Select all
DirectoryIndex cover.html
AddType text/x-component .htc

Options +FollowSymLinks

RewriteEngine On

RewriteBase /slush
RewriteCond %{REQUEST_URI} ^/sub(/.*)$ [NC]
RewriteRule ^/sub(/.*)$ /sub2(/.*)$1 [QSA,L]
and various combinations all giving the same 404 error:
File does not exist: P:/website/upload/slush/sub

This locked sticky post at the modrewrite forums is helpful:
http://forum.modrewrite.com/viewtopic.php?t=1445

Perhaps try posting your Rewrite query on http://forum.modrewrite.com and see if guru richardk perhaps can suggest the correct entries for your particular needs, if you still want to use the .htaccess that is.

BTW, Apache uses Perl Compatible Regular Expressions provided by the PCRE library - quote from the Apache Glossary under Regular Expression (Regex)

Seymour Clufley wrote:Would it be helpful if I pasted the entire httpd.conf file?
No need if you move your Alias to the httpd-xampp.conf file but I would just like to check your Alias to make sure it is OK and the one I posted should work for you - let me know how you get on please.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Please help with RewriteEngine in htaccess file

Postby Wiedmann » 24. January 2009 11:12

Would it be helpful if I pasted the entire httpd.conf file?

No. Just read my post above...
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: Please help with RewriteEngine in htaccess file

Postby Seymour Clufley » 26. January 2009 03:07

Izzy, thanks for your very helpful post. It's good of you to give your time like this!

However, I've decided to attempt an alternative route for now. I'm going to try using a RewriteMap and an external program to do the URL rewriting. Although it would be the ideal solution, it seems to be very leftfield and might not work. Nonetheless, I've got to try!

You asked for the slush alias section from my httpd.conf file:
Code: Select all
Alias /slush "P:/website/upload"
<Directory "P:/website/upload" >
Options Indexes FollowSymLinks +Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
If there's anything wrong with that, please let me know.
Seymour Clufley
 
Posts: 11
Joined: 16. March 2008 09:40

Re: Please help with RewriteEngine in htaccess file

Postby Izzy » 26. January 2009 03:38

Your welcome and I just would like to see a solution to this issue.

Your Alias directive should ideally be removed from the httpd.conf file and put into the xampp\apache\conf\extra\httpd-xampp.conf file with all the others, after say the phpMyAdmin Alias and should follow the same pattern as the phpMyAdmin Alias - save the file and restart Apache as with all ini and conf edits in XAMPP.

As you can see in that file all Alias directives are contained within:
<IfModule alias_module>
</IfModule>

If this was not the case in the httpd.conf file then it may not have worked as intended.

BTW I have tried with leading slashes and without leading slashes and every combination that I am aware of in the .htaccess rewrite rule without any success - no matter what I tried I would always receive that damn 404 error.

.htaccess with the Alias and without the Alias, the Alias I posted without a .htaccess worked fine.

I will also take a look at RewriteMap but what external program do you have in mind?
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Please help with RewriteEngine in htaccess file

Postby Wiedmann » 26. January 2009 08:47

To recap, I'm using Xampp on an offline PC, testing a site stored in "P:\website\upload\". That's the root directory. It is set up in httpd.conf as an alias, called "slush".

So you have:
Code: Select all
Alias /slush "P:/website/upload/"
<Directory "P:/website/upload">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

in your "httpd.conf".

The URL is supposed to get rewritten to "http://localhost/slush/sub2/test.html". The sub2 directory exists, as does the test.html inside it. (There is no "sub" directory, only "sub2".)

So you need:
Code: Select all
RewriteEngine On
RewriteBase /slush
RewriteRule ^sub/(.*)$ sub2/$1

In your ".htaccess" in "P:\website\upload".
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: Please help with RewriteEngine in htaccess file

Postby Seymour Clufley » 26. January 2009 16:37

Thank you again, Izzy and Wiedmann.

I want to clarify something. The alias was working fine for me, except when it came to URL rewriting. For example if I pointed a browser to:
http://localhost/slush/apage.html
it would open
P:\website\upload\apage.html
I was also able to use CGI executables in the upload folder to handle Ajax duties and everything just fine. It's the URL rewriting that doesn't work.

The executable I want to use for RewriteMap is my own one, which I'm writing in PureBasic. I'm between "intermediate" and "advanced" with that language so I'd much prefer to use it for rewriting duties than Mod_Rewrite, if possible.

RichardK hasn't got back to me yet about RewriteMap so in the meantime I'll try your suggestions for a simple sub->sub2 rewrite.
Seymour Clufley
 
Posts: 11
Joined: 16. March 2008 09:40

Re: Please help with RewriteEngine in htaccess file

Postby Izzy » 26. January 2009 23:42

I'll try your suggestions for a simple sub->sub2 rewrite.
That suggestion still produced a 404 error only this time it could not locate the folders in the P:\website\upload\ folder instead of the P:\website\upload\slush folder - and I used with the Alias and without the Alias etc.

I had already tried that suggestion previously with the same 404 result but you may have better luck. :)
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Next

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 116 guests