Rewrite links with htaccess

Description:

Rewrite or redirect links insite a web application.

Solution:

Redirection is simple using htaccess. Just create a file named .htaccess in the root of your web project and with some basic rules and regular expressions, all calls to your webiste can be redirected.

Example:

RewriteEngine on
RewriteRule ^item-([0-9]+)\.html$ item.php?id=$1 [NC]
RewriteRule ^(.*)\.jpg$ /view.php?image=$1 [NC]

First we enable the rewriting mode, and then start defining the rules. First example will take a link of sort “item-xx” where “xx” is a number and convert it to a link of the type “item.php?id=xx”. Second example searches for links that contain a jpeg image and convert the image name into a parameter for the URL.

The flag at the end, [NC], defines that the link will be considered regardless of the case. Other flags can be defined as well, a full list can be seen here.

Advertisements

About admin

Just another php developer trying to give something back to the community.
This entry was posted in System, Tools and tagged , . Bookmark the permalink.

7 Responses to Rewrite links with htaccess

  1. Pan says:

    Hi,

    How can i redirect a user if he/she presses cancel in the login window?

    PA

    • justbugs says:

      Hi,
      Depends what is the action triggered by the cancel button. Usually it clears the login form.
      But if you have the action url and you know which url you want it to be redirected at, you can easily create the rule.
      However, why don’t you add the redirection directly in the cancel action.
      Regards

  2. Pan says:

    Hi,

    Lets say that i want to redirect the user to an imaginary website http://www.mywebsite.com, can you please show me how to edit my .htaccess?

    How to add the redirection directly in the cancel action, can you show me?

    PA

    • justbugs says:

      You don’t need .htaccess for that.
      You can do ti with javascript, adding an onClick event on the button like this:

      Or directly with anchor, but not as nice:

  3. Pan says:

    No,

    It does not work like that. I want to use the .htaccess and modify it in a way to redirect the user if he/she presses the cancel button when the Authentication Required window pops up, no JavaScript at all. I am pretty close to do that, I have to modify the ErrorDocument 401 and ErrorDocument 404, i will send you the solution once i do it, actually i have done it for a localhost page but i want to do on a live webiste.

    PA

  4. Pan says:

    All you need to do is include inside the .htaccess file lines like these:

    ErrorDocument 401 /redirect/redirect.html
    ErrorDocument 404 /redirect/redirect.html

    Cheers,

    PA

  5. Pingback: Difference between 301 and 302 redirect | justbugs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s