Adding Ignore list in Apache .htaccess

Hi

I currently have an .htaccess file and what it does is, when someone types the url [noparse]http://example.com/username[/noparse], it calls the user_profile.php page in the backend and shows the user’s profile for the username supplied.

The contents of the ,htaccess file is


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ user_profile.php?id=$1& [L,NC]

The code works fine so far only that when someone types [noparse]http://example.com/blog[/noparse] or [noparse]http://example.com/forum[/noparse] it treats them as the username.

I was wondering if we can add an ignore list or something in the .htaccess file for the following

/blog
/forum/
and all php files i.e. *.php

is this possible?

Any help will be appreciated.

Thanks in advance

Are blog/ and forum/ real directories?

If so, you could do


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ user_profile.php?id=$1& [L,NC]

That disables rewriting for all real directories.
If they are not [real directories] you could do


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(blog|forum)
RewriteRule ^([^/]+)/?$ user_profile.php?id=$1& [L,NC]

HTH :slight_smile:

BTW. If you have a strict set of characters that usernames can consist of, like only letters, digits and dashes, change ^([^/]+)/?$ to the stricter version ( for my example that would be ^([a-zA-Z0-9-]+)/?$ )

Thanks for that prompt reply.

  1. Yes blogs and forums are real directories.

  2. What if there is a subdomain like http://mail.example.com, it should not be effected by the htaccess, will it?

  3. How do I ignore the existing .php files, like help.php, contactus.php, aboutus.php, can we ignore all .php files?

Thanks once again

Okay, than you can use the !-d option I showed you

Short answer: no
Longer answer: the .htaccess only affects Apache, so only calls to your website. All other protocols like mail (smtp, pop3, imap), ftp, ssh, etc. are not affected by the .htaccess.

That’s what RewriteCond %{REQUEST_FILENAME} !-f does

Let me comment the .htaccess for you


# Start the RewriteEngine, so we can use it
RewriteEngine On

# If the request is not (as per !) for an existing file (as per -f) ...
RewriteCond %{REQUEST_FILENAME} !-f

# ... and the request is not (as per !) for an existing directory (as per -d) ...
RewriteCond %{REQUEST_FILENAME} !-d

# ... redirect (request-uri) to user_profile.php?id=(request-uri)
RewriteRule ^([^/]+)/?$ user_profile.php?id=$1& [L,NC]

Does that make it clear?

PS. Just saw the [NC] on that RewriteRule. Since ^([^/]+)/?$ is case insensitive by default, you can drop that [NC].

oh wow thanks for that detailed explanation.

Just curious what does [L,NC] do?

[L,NC] is a combination of [L] and [NC]

[L] stands for Last, and basically says: stop processing the rules and go back to the start of the .htaccess. Normally Apache would process all rules in the .htaccess and then go back to the start, if the URL that came out of the .htaccess is different than the one that went in.
So you use [L] to make the .htaccess go faster, or take a shortcut - so to speak

[NC] stands for NoCase, which is to say “make this rule case insensitive”.
So if you have a rule for example like


RewriteRule ^test$ index.php [L]

that will match “test”, but it won’t match “Test”, “tEst”, “teSt”, “tesT”, “TEst”, etc

However


RewriteRule ^test$ index.php [L,[B]NC[/B]]

will match “Test”, “tEst”, “teSt”, “tesT”, “TEst”, etc

Thanks man for all your help.

I shall try this and get back to you if I face any problem.

God Bless!

Hi Again

Is there also a way to have the video id after the username ?

like

example.com/john/11526

In the above URL john is the username and 11526 is the video id.

I modified the .htaccess and this is how it looks like


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?([^/]+)/?$ user_channel.php?id=$1& [L,NC]

All works well except that the images on that page are broken. Do you think the rule in the above .htaccess is fine?

  1. The images works fine if I remove the video id
  2. The images have relevant path and for some reason I cannot provide absolute path.

Thanks

The thing is that you now have created an extra “directory”, so relative links don’t work anymore.
See section “Relative Links Are Missing!” on Apache’s mod_rewrite