Eda190
Eda190

Reputation: 679

.htaccess file RewriteRules not working

This question was answered numerous times on StackOverflow, and I really did search alot, but no answers seem to work for me. I've read this article, and found out why adding Options +FollowSymlinks, which is supposed to be necessary for RewriteRules to work doesn't work for me (my host has it automaticaly enabled on root level). So I removed it from my file, and tried if Rewrite engine even works. It does, but NONE of Regex stuff does. So I set up a test website at http://example.com/hta.php?id=something&jmeno=somethingelse, and added the following to my .htaccess file:

RewriteEngine On

RewriteRule ^testdis/([0-9]+)/([a-z]+) http://example.com/hta.php?id=$1&jmeno=$2 [NC]

This should make me be able to acces http://example.com/testdis/5/s and work like previously mentioned http://example.com/hta.php?id=5&jmeno=s, but it rewrites the adress to second, unwanted adress in URL tab.

So my question is: Why doesn't my rewrite rule work, and how do I make it work?

EDIT: Adding [R] parameter makes me able to type the "nice" url into my browser and upon presing enter it redirects me to "old/ugly" page with working values. However I want it to show the "nice" url.

EDIT 2: I was asked to post the code of my hta.php file. I don't think it anyhow matters, but here it is

<?php
  echo '<meta charset="UTF-8">';
  if(isset($_GET['id'])){
  $id = $_GET['id'];
  }
  if(isset($_GET['jmeno'])){
  $jmeno = $_GET['jmeno'];
  }
?>
<!DOCTYPE html>
<html lang="cs">
    <head>
    <title>Test</title>
    </head>
    <body>
  <?php echo $id; ?>
  <br /><br />
  <?php echo $jmeno; ?> 
  </body>
</html>

Upvotes: 2

Views: 346

Answers (1)

MrWhite
MrWhite

Reputation: 45829

For an internal rewrite the RewriteRule substitution should not contain the scheme and hostname, so the following is preferable (and appears to solve the problem in this instance):

RewriteRule ^testdis/([0-9]+)/([a-z]+) /hta.php?id=$1&jmeno=$2 [L]

Normally, if you specify an absolute URL it will implicitly trigger an external redirect (as if you had explicitly used the R flag). Although, the docs state that if the hostname matches the current host (which as far as I know it does in this instance) the rewrite engine should automatically strip the scheme and hostname from the substitution.

Reference:
http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

However, in my experience this does not appear to happen, an absolute URL in the substitution always results in an external redirect, regardless of the hostname. (?)

Upvotes: 1

Related Questions