user2874853
user2874853

Reputation:

Dot in .htaccess not working and weird redirect to server path

I 'm trying to get my .htaccess File work. But it seems to have a few problems:

RewriteEngine on
RewriteRule ^css/ - [L]
RewriteRule ^img/ - [L]
RewriteRule ^js/ - [L]

RewriteCond %{HTTP_HOST} ^www\.example\.ch$ [NC]
RewriteRule ^(.*)$ http://example.ch/$1 [R=301,L]

RewriteRule ^(dir1/dir2)([a-zA-Z0-9\/\-\_,]+)$ index.php?page=$1&id=$2 [L]
RewriteRule ^(dir1/dir2)([a-zA-Z0-9\/\-\_,]+)$ index.php?page=$1&id=$2 [L]
RewriteRule ^(dir1)([a-zA-Z0-9\/\-\_,]+)$ index.php?page=$1&id=$2 [L]
RewriteRule ^([a-zA-Z0-9\/\-\_,]+)$ index.php?page=$1 [L]

1) In my .htaccess the dot doesn't work. When i write the last rule like this:

RewriteRule ^(.*)$ index.php?page=$1 [L]

I just get the 'home' page. If I var_dump($_GET) there's the key 'page', as it should, but it's value is 'index.php'. How comes, it doesnt contain whats written in the address?

2) There was an older Version of this Website with URLs like www.example.ch/page.php. I want to redirect the old .php sites to the new Website, so visitors which click on an old link in google won't get an 404 error. When I type in example.ch/page.php, with the above .htaccess i get redirected to something like:

 http://example.ch/home/httpd/vhosts/example.ch/httpdocs/page

What's the reason for that?

Thank you

Upvotes: 3

Views: 1634

Answers (1)

Steven
Steven

Reputation: 6148

Question 1

This ones an easy fix... It's because your .htaccess file is rewriting the rewritten url. To illustrate:

http://mysite.com/this/content                     <---Original URL
http://mysite.com/index.php?key1=this&key2=content <---First rewrite
http://mysite.com/index.php?key1=index.php         <---Second rewrite/final url

To fix this you can simply add a rewrite condition to not rewrite if the file exists like:

ReWriteCond %{REQUEST_FILENAME} !-f

Edit

Just for completeness... The reason why

RewriteRule ^([a-zA-Z0-9\/\-\_,]+)$ index.php?page=$1 [L]

works and

RewriteRule ^(.*)$ index.php?page=$1 [L]

doesn't is because .* includes the characters ? and = where as your pattern doesn't (the ? and = coming from the GET string).

Question 2

To convert http://site.com/file.php to http://site.com/file` you can use the following rewrite rules:

ReWriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^(.*).php$ $1

Upvotes: 2

Related Questions