Cole
Cole

Reputation: 459

.htaccess mod rewrite for two pages

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /me/profile.php?username=$1 [QSA,L]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /me/index.php?page=$1 [QSA,L]

The second rewrite for index page isn't working, but however, the first for the profile page works.

This is what I used in my index.php:

    if(isset($_GET['page']) === true && empty($_GET['page']) === false){

        if($_GET['page'] == 'Home'){
?>
<div id = "contents">
Works!
</div>
<?php 
}else{
<div id = "contents">
Error page!
</div>
}
}
?>

So what is the problem with the index page. How can I rewrite for both of them?

Upvotes: 0

Views: 114

Answers (1)

James Coyle
James Coyle

Reputation: 10398

The problem is that you catch anything in the URL and redirect to the profile.php page not allowing it to get to the index.php line. Try something like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/profile/(.*)$ /me/profile.php?username=$1 [QSA,L]
RewriteRule ^(.*)$ /me/index.php?page=$1 [QSA,L]

That way the profile page can be differentiated from the index page and rewritten correctly. Eg. Domain.com/profile/jimjimmy1995 for the user page and domain.com/about for the other pages.

Upvotes: 1

Related Questions