简体   繁体   中英

Vanity Profile url using htaccess with PHP

I have .htaccess file, profile.php , index.php and test.php .

Let's say we have user john

If I will go to website http://example.org/john , then I can see john profile. But If I will go to index.php or test.php , then pages are with error 404 but shouldn't be. It seems that code from htaccess file is crashing every page.

How I can fix profile.php and .htaccess file to get:

  1. If http://example.org/john doesn't exist, then show 404 error on the same page, if exist - show this profile
  2. Other pages should display if they exist (like test.php )

Code:

.htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off


RewriteRule ^([A-Za-z0-9\._\-]+)[^.]$ profile.php?user=$1 [NC]


RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

profile.php

<?php
if ($_GET['user'] != '') {

    echo 'Showing user profile';

} else {

    echo '404 error';

}

You cannot go from php execution back to apache.

You should swap the RewriteRules, so the file to php is performed first and the profile is done second.

Add L after your rule of no additional rewrite is required.

Look at http://php.net/manual/en/function.header.php to send a proper 404 response. Set 404 as the third argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM