Reputation: 623
Hi,
I am in the middle of a project and I am using Mod_Rewrite and .htaccess to change some URLS. I want the file extension to be removed but once the user is signed in, they go to a folder called portal and then, /portal/index.php runs all the pages.
Here is the code I currently have:
RewriteRule ^(.*)$ $1.php
RewriteRule ^portal/(.*)$ portal/index.php?page=$1 [L,QSA]
This is not working, but I feel that I am close to the end result I'm hoping for.
The URL for pages should be like:
site.com/home
site.com/otherpage
and so on. These should be sent to:
/home.php
/otherpage.php
etc.
But once the user is logged in, the URL:
site.com/portal/index.php?page=section/section2/so-on
Should be re-written to:
site.com/portal/section/section2/so-on
With what I have, the first rule works, but the rest does not.
Upvotes: 1
Views: 153
Reputation: 785681
Always keep the order of the rules from most specific to most generic in .htaccess. Your complete .htaccess should be:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^portal/(.*)$ /portal/index.php?page=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Explanation:
Rule 1 is simple. It is forwarding every /portal/foo
URI which is not a physical file to /portal/index.php?page=foo
. Flags L for last rule, QSA is for appending existing query string and NC is for ignore case matching.
Rule 2 is forwarding a URI like /bar/
to /bar.php
if it is not a real directory and there is actually a file calle /bar.php
. Rexex ^(.*?)/?$
is there for stripping out trailing slash (note /bar/
goes to /bar.php
).
Upvotes: 1
Reputation: 690
You need to reorder the rules as the first one sends /portal/section/section2/so-on to /portal/section/section2/so-on.
It needs to be:
RewriteRule ^(.*)$ $1.php
RewriteRule ^portal/(.*)$ portal/index.php?page=$1 [L,QSA]
Upvotes: 0