September 12, 2013

Using mod_rewrite to make Blog URLs more SEO Friendly

Three3’s Question:

I am trying to turn my blog URLs into a more SEO friendly format using mod_rewrite. All of my articles are stored in a simple MySQL database. Each blog article url looks like this:

I have managed to to make them look like this using mod_rewrite:

Here is my code that I paced in my .htaccess file:

RewriteRule ^blog/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$ /blog?id=$3&category=$1&pagename=$2 [L]

So what happens is this: When I click on the URL, all of my CSS and images are not loading because it is trying to load them from a directory that does not actually exists. How would I load the files without having to create multiple directories that contain my sites CSS files and images?

Use root identifier / in your path. This will point the DocumentRoot of your server. Let me explain How this works

For an image like this:

<img src='test.jpg' /> 

Browser/Server will find it as' but if you use / before the path

<img src='/test.jpg' />

It will look for it in

Another Technique

Use full path in your files like:

<img src='' />
July 2, 2013

htaccess rule cannot override previous rule

MrN’s Question:

I have a bunch of htaccess rule setup on website.

RewriteEngine On
RewriteBase /
RewriteRule ^google73947f9bfca52abe.html$    google73947f9bfca52abe.html [L]
RewriteRule ^page/(.*)$ index.php?title=page&r=$1 [L]
RewriteRule ^media/(.*)$ index.php?title=page&r=$1 [L]

RewriteRule ^admin/$    admin/php [L]
RewriteRule ^index.html$    index.php [L]
RewriteRule ^crons/$    crons/ [L]

#I did this to forward request to directory    
RewriteRule ^sites/$    sites/ [L]

#This  is the rule creating problem
#RewriteRule ^sites/([^.]+)$    sites/$1 [L]

RewriteRule ^([^.]+).html$   /index.php?title=$1
RewriteRule ^(.*).htm       index.php?action=$1 [L]RewriteRule ^([^.]+)$   /index.php?title=$1

I want to override every rule for those that start with to go the directory instead of going to index.php as it configured to do so.

Whenever I go to the sites takes me to but I can still browse it as folder but Whenever I open a html file like lets say test.html it takes me directly to index.php on earlier directory as it is suppose to.

How to write rule which lets me browse a directly freely?

You have to configure to omit all the rules when the line sites is encountered.

Here is your solution:

RewriteEngine On
RewriteRule ^(sites) - [L]

#Rest of your rules
June 18, 2013

Disable direct access to files in PHP

Mr Me’s Question:

I’ve already checked the issue disable access to included files , but I am wondering if that’s the best solution.

Context: I’m building a bootstrap class for my PHP Framework, and realized that there are multiple solutions to this security issue.

After researching and reading posts like the one I mentioned at first and others related to htaccess, I think that there are basically three types of solutions:

1 – Checking a constant (like in the post I linked)

if(!defined('THIS_IS_NOT_A_DIRECT_ACCESS')) {
    header('HTTP/1.1 404 Not Found');


    header('HTTP/1.1 404 Not Found');

2 – Redirecting all calls to the Bootstrap and making a clever filtering.

//Something like
// if $urlRequested it's a file
    // Go to Error
// else if $urlRequested it's not a controller
    // Go to Error
// else 
    // Execute Controller Logic.

3 – Setting htaccess.

# Redirecting calls for non-directories, files or links
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA]

# Redirecting the rest of the calls to the error page.
RewriteRule ^(.+)$ index.php?url=error/404 [QSA]

Personally I think the solution 3 is the most interesting, but I am pretty new in the Htaccess control so I wonder if this is a safe solution.

For the purists and minimalists, the Question in here would be:
Are these (the three examples) nice direct access control systems for Apache-PHP applications? Else, which would be the safest approach? And the simplest?

This is a debatable topic but .htaccess rules applies to all the document on that particular directory. While applying 1 or 2 you may have to include that portion on every file.

May 29, 2013

mod_rewrite, Adding a Condition

A A’s Question:

I have this .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ zone.php [L]

Right now it works pretty well. How can I make this RewriteRule not work in a certain directory?

I do not want the rule to work for anything in

Change your code with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule (?!^manage/)^.*$ /zone.php [L,NC]

Add a condition by using the RewriteCond keyword to apply the rule if the directory is not manage

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/manage(/|$)
RewriteRule ^(.*)$ zone.php [L]
March 9, 2013

Running a Zend Framework Project on a shared server

Question by Mikey

I’m trying to upload my ZF Project to shared hosting

On my XAMPP, ZF’s index page is located (and I access my page) at http://localhost/ZFprojectname/public

On the shared hosting in the root directory I have installed Joomla.

I want to access my ZF in the manner of

so in this case, when going to I should be accessing ZF’s public folder (as far as I understand).

And, I’d like to put my ZFproject in public_html/somefolderName/

How would you do it?

Answer by Starx

Shared hosting do not support defining Document Root path so you can use .htaccess to forward the request to public folder instead.

Create a .htaccess file inside the booking directory with the following rule.

RewriteEngine On

RewriteRule ^.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ /public/index.php [NC,L]
November 16, 2012

Block access to files and sub directory with .htaccess?

Question by jaya malladi

I have nearly 30 php files, 4 sub directories in my directory. I want to block some php files and sub directories from user in direct viewing like

My currect htaccess file

## Enable Mod Rewrite, this is only required once in each .htaccess file
RewriteEngine On 
RewriteBase / 
## Test for access to includes directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /includes/ .*$ [NC] 
## Test that file requested has php extension 
RewriteCond %{REQUEST_FILENAME} ^.+.php$ 
## Forbid Access 
RewriteRule .* - [F,NS,L]

How to do it?

Answer by Starx

You can filter the files like this

<Files ~ ".php$">
  Deny from all

And directory like this

<Directory "/subdirectory">
  Deny from all
October 3, 2012

Rewrite rule in htaccess for mobile version

Question by Soumya Mohanan

I am using codeigniter framework for my site and I have done two site one for accessing through mobile and another for accessing through pcs. both are in same domain . My site url is like this http://mydomain.comAnd my mobile is I need to write htaccess rule which redirect to site url when i am accessing site from mobile and i need to rewrite to when i am accessing the site from pc.

How can i write rewrite rule in such case ,please help any help will be appreciated.

Answer by Starx

As scessor, said, it cannot be done with .htaccess. You need to analyse the browser’s user agent for this.

Check out this mobile detection in php.

Usage is as easy as this

if ($detect->isMobile()) {
October 1, 2012

PHP .htacces and index.php redirection

Question by Emann Tumala Saligue

I am new about this redirection and I want to know how it works?
I have some file called index.php and i want to hadlle it any directory in the site



The url is:

test 1. - will view the include home.
test 2. - will view the include  settings.
test 3. - will view the include   404 page.

How to make .htaccess and index.php using that code or any idea how it works and sample code of it.

Answer by Starx

I will try to explain this with a simple example. Consider the following .htaccess file

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /index.php?x=$1 [L] 
#  ^ This is the rule that does the redirection

What it does is, it routes every url request and sends the request to index.php. How it send it? I will show couple of example to do that.

  • will be send as
  • will be send as

So, now you are configure your index.php and decided what you want to do, with the value you get in $_GET['x']

switch($_GET['x']) {
   case "profile": include("include/profile.php"); break;
   // .... similarly other cases
   default: include("includes/home.php"); break;


May 3, 2012

error in 301 permanent redirect

Question by asitha

i am using htaccess for 301 permanent redirect.
i have to make a redirect for bima.php to i wrote the below code in htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^bima.php?$ $1/ge.php [NC,R=301,L]

this works properly..whenever i put in url it will redirect to
the problem is i have to do a 301 redirect for ge.php also.that means whenever in url it will redirect to needs to redirect to and vice versa.
Any idea?or anyway to do this?

Answer by Starx

Your redirect rule

RewriteRule ^bima.php?$ $1/ge.php [NC,R=301,L]
RewriteRule ^ge.php?$ $1/bima.php [NC,R=301,L]

Is redirecting in infinite loop. Remove one of them.

No matter what type of logic you use, the redirection you are attempting will end of in loop at one time. So better avoid the need of such requirement.

Here is a PHP Solution

File: ge.php

if($_SESSION['redirected']['from'] != 'bima.php') {
   header("location: bima.php");
   $_SESSION['redirected']['from'] = 'ge.php';

File: bima.php

if($_SESSION['redirected']['from'] != 'ge.php') {
   header("location: ge.php");
   $_SESSION['redirected']['from'] = 'ge.php';
April 23, 2012

Single page or multiple pages?

Question by Ferenc Kamraš

I want to make a web service where users can make their own pages. Which is the better solution:

a. Execute everything on one page with get variables, using clean urls


b. Make seperate .php files for every user

Is it a problem to have all the traffic of 500-1000 users on one page?

Answer by Your Common Sense

Second is not an option at all. It just makes no sense in the context of the dynamic web pages

Go for the first.
this is how every web site in the world works

Answer by Starx

A different way to look up what Your Common Sense has said.

Imagine you DID applied the second options, then suddenly your website booms like facebook. What do you think you will do, create over millions of pages for each user.

That is one of the worst concept NO OFFENCE, create a common page for every user to land on, control the content and elements as per them.


Please fill the form - I will response as fast as I can!