Yannick Bloem
Yannick Bloem

Reputation: 147

.htaccess works in local but not on server (but no 404 error)

I have a .htaccess that is supposed to rewrite my URL. My host has told me that it supports URL rewriting, and I verified that by using phpinfo() and checking.

Anyways, this is my .htaccess:

RewriteEngine On

RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]

It works like a charm in local, but on my server, it doesn't do anything.

I checked this before on the internet and some people had it, but they all had a 404 error, while I don't have a 404 error. It simply doesn't redirect, it doesn't do anything, so I get all kind of error messages.

Upvotes: 0

Views: 131

Answers (2)

AbsoluteƵERØ
AbsoluteƵERØ

Reputation: 7870

You can make sure that the file (!-f) or directory (!-d) that you're matching doesn't exist before the rewrite. That way you don't end up with a 500 loop with something like /index.php?page=index. Additionally the ^ character is matching the beginning of the string, so if your original test was in a subdirectory it would not rewrite since you weren't allowing slashes.

This should work for any instance, however it will ONLY make the page variable the last string in the URI.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([_a-zA-Z0-9]+)$ /index.php?page=$1 [R,L]

Upvotes: 0

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]

The regex in your rule doesn't match strings with slashes at any position. I am not sure that's acceptable and you don't give any request examples, but I don't think it is.

You may try this rule-set in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1    [L]

For permanent redirection replace [L] with [R=301,L].

Upvotes: 1

Related Questions