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.
Hi,
How can i redirect a user if he/she presses cancel in the login window?
PA
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
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
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:
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
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
Pingback: Difference between 301 and 302 redirect | justbugs