DemCodeLines
DemCodeLines

Reputation: 1920

Apache Rewrite rule with multiple files from same virtual directory?

There are two parts to this problem:

  1. I have used htaccess rewrite to change "file.php" to just "file" so http://www.example.com/file works just like http://www.example.com/file.php

Here is part of my .htaccess that deals with that:

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule (.+) /$1.php [L]
  1. The second part is the directory problem:

Here is part of my .htaccess that deals with the issue:

    RewriteRule ^register website/register

    RewriteRule ^register/rules website/rules

What I am trying to accomplish is this:

http://www.example.com/register uses the rewrite rule to show website/register http://www.example.com/register/rules uses the rewrite rule to show website/rules

The problem is that the URL box shows example.com/register/rules but the page continues to show the content of example.com/register.

What is going wrong here? Is it the ".php" extension is the first part that is causing all this? Or is my htaccess just completely wrong?

Upvotes: 2

Views: 1097

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

Nothing is happening. The URL box shows example.com/register/rules but the page continues to show the content of example.com/register

This is happening because you aren't bounding the rules, you have this:

RewriteRule ^register website/register

RewriteRule ^register/rules website/rules

The 2nd rule never gets applied because the URI /register/rules, matches the first rule's regex (^register, meaning, "the URI starts with register"). Either add ending boundaries:

RewriteRule ^register/?$ website/register [L]

RewriteRule ^register/rules/?$ website/rules [L]

or swap them around:

RewriteRule ^register/rules website/rules [L]

RewriteRule ^register website/register [L]

Though you will need the [L] flag.

Upvotes: 1

Related Questions