Category Archives for HTACCESS

Smarty with htaccess

If you are using PHP with SMARTY  and want to quickly convert your ugly URLs to pretty URLs, it is best to use a detailed ruleset for mod_rewrite to process. The alternative is to say “if the requested file does not exist, rewrite to a particular script”. This means that requests for images, CSS files, etc. will still return the correct file. Everything else will be sent to the script to process where the requested URL will be available under the server REQUEST_URI variable. To use this method, you would need to create a .htaccess file in your domain root (or wherever you want the rewrite to apply) with the following inside:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]

Replace index.php with the name of whatever script you wish to use. Sticking with the PHP example, I could then create the index.php file as follows:

<?php
$requested = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI'];
 
$smarty=new Smarty();
 
switch ( $requested ) {
 
        case 'latest':
               require_once 'latest_products.php';
               $content=$smarty->fetch(latest.tpl);
               break;
 
        case 'listing':
               require_once 'list_products.php';
               $content=$smarty->fetch(list_products.tpl);
               break;
 
        default:
               include 'default.php';
               $content=$smarty->fetch(default.tpl);
}
 
$smarty->assign(‘header’,$smarty->fetch(header.tpl));
$smarty->assign(‘footer’,$smarty->fetch(footer.tpl));
$smarty->assign(‘bodyContent’,$content);
$smarty->display(main.tpl);
 
?>

 
main .tpl

 {$header}

{$bodyContent}

{$footer}

 

If you need any projects or customization in SMARTY and PHP  , please send me an message via contact form

URL rewriting using php and htaccess

If you are using a particular script and want to quickly convert your ugly URLs to pretty URLs, it is best to use a detailed ruleset for mod_rewrite to process. The alternative is to say “if the requested file does not exist, rewrite to a particular script”. This means that requests for images, CSS files, etc. will still return the correct file. Everything else will be sent to the script to process where the requested URL will be available under the server REQUEST_URI variable. To use this method, you would need to create a .htaccess file in your domain root (or wherever you want the rewrite to apply) with the following inside:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]

Replace index.php with the name of whatever script you wish to use. Sticking with the PHP example, I could then create the index.php file as follows:

<?php
$requested = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI'];

switch ( $requested ) {

	case '/dogs':
		include 'animals/dogs.php';
		break;

	case '/apple':
		include 'fruit.php?type=apple';

	default:
		include '404.php';
}

?>

Remember, the REQUEST_URI variable will contain everything after the hostname and should start with a leading slash. You will need to process the request a bit better than I have, but the reason you are using this method is because you are comfortable with your server side scripting!

Note : This is the base how permalinks in wordpress works.

htaccess tutorial for beginners

Module Rewrite

This article describes how one can use Apache’s mod_rewrite to solve typical URL based problems. The Apache module mod_rewrite is a module which provides a powerful way to do URL manipulations. With it you can nearly do all types of URL manipulations you ever dreamed about. The price you have to pay is to accept complexity, because mod_rewrite is not easy to understand and use for the beginner.

NOTE: Depending on your server configuration it can be necessary to change the examples for your situation. Always try to understand what it really does before you use it. Bad use would lead to deadloops and will hang the server.

The most example’s can be used in the .htaccess file while other ones only in the Apache htppd.conf file.

RewriteCond

The RewriteCond directive defines a rule condition. Preserve a RewriteRule with one or more RewriteCond directives. The following rewriting rule is only used if its pattern matches the current state of the URI and if these additional conditions apply too.

You can set special flags for condition pattern by appending a third argument to the RewriteCond directive. Flags is a comma-separated list of the following flags:
[NC] (No Case)
This makes the condition pattern case insensitive, no difference between ‘A-Z’ and ‘a-z’.

[OR] (OR next condition)
Used to combinate rule conditions with a OR.

RewriteRule

The RewriteRule directive is the real rewriting.

You can set special flags for condition pattern by appending a third argument to the RewriteCond directive. Flags is a comma-separated list of the following flags:
[R] (force Redirect)
Redirect the URL to a external redirection. Send the HTTP response, 302 (MOVED TEMPORARILY).

[F] (force URL to be Forbidden)
Forces the current URL to be forbidden. Send the HTTP response, 403 (FORBIDDEN).

[G] (force URL to be Gone)
Forces the current URL to be gone. Send the HTTP response, 410 (GONE).

[L] (last rule)
Forces the rewriting processing to stop here and don’t apply any more rewriting rules.

[P] (force proxy)
This flag forces the current URL as a proxy request and put through the proxy module mod_proxy.

Regular expressions

Some hints about the syntax of regular expressions:

Text:
. Any single character
[chars] One  of chars
[^chars] None of chars
text1|text2 text1 or text2

Quantifiers:
? 0 or 1 of the preceding text
* 0 or N of the preceding text (N > 0)
+ 1 or N of the preceding text (N > 1)

Grouping:
(text) Grouping of text

Anchors:
^ Start of line anchor
$ End of line anchor

Escaping:
\ char escape that particular char

Condition pattern

There are some special variants of CondPatterns. Instead of real regular expression strings you can also use one of the following:

< Condition (is lower than Condition)
Treats the Condition as a string and compares it to String. True if String is lower than Condition.

> Condition (is greater than Condition)
Treats the Condition as a string and compares it to String. True if String is greater than CondPattern.

=
Condition (is equal to Condition)
Treats the Condition as a string and compares it to String. True if String is equal to CondPattern.

-d
(is directory)
Treats the String as a pathname and tests if it exists and is a directory.

-f
(is regular file)
Treats the String as a pathname and tests if it exists and is a regular file.

-s
(is regular file with size)
Treats the String as a pathname and tests if it exists and is a regular file with size greater than zero.

-l (is symbolic link)
Treats the String as a pathname and tests if it exists and is a symbolic link.

-F (is existing file via sub request)
Checks if String is a valid file and accessible via all the server’s currently configured access controls for that path. Use it with care because it decreases your servers performance!

-U (is existing URL via sub request)
Checks if String is a valid URL and accessible via all the server’s currently configured access controls for that path. Use it with care because it decreases your servers performance!

NOTE: You can prefix the pattern string with a ‘!’ character (exclamation mark) to specify a non-matching pattern.