We help IT Professionals succeed at work.

htaccess - redirect text links that don't exist

sabecs
sabecs asked
on
Hi,
is it possible to use .htaccess to redirect text links that don't exist to specific page.

for example I would like
"Online Shop" to redirect to http://www.mywebsite.com/index.php?page_id=154 
"Contact" to redirect to http://www.mywebsite.com/index.php?page_id=12 

Thanks in advance for your feedback.
Comment
Watch Question

Hello sabecs,

It is possible to redirect text links that don't exist to a specific page.

Go through following links. It will help you to learn about rewrite urls.

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

http://www.evolt.org/ultimate_htaccess_examples

http://www.webweaver.nu/html-tips/web-redirection.shtml

Hope this will help you.

Thank You.

Amar Bardoliwala.
Could you please be more specific? Are these the rewrites you want to acheive?:

http://www.mywebsite.com/Online_Shop -> http://www.mywebsite.com/index.php?page_id=154 

http://www.mywebsite.com/Contact ->  http://www.mywebsite.com/index.php?page_id=12

If so, use the following ruleset:


RewriteEngine On
RewriteRule ^Online_Shop index.php?page_id=154
RewriteRule ^Contact index.php?page_id=12

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
You might want to have a general-purpose "missing page" handler.  I use this in my .htaccess

ErrorDocument 404 /404page.php

In the 404page.php script, I look at the arguments in the client's URL and try to make an educated guess about what the client really wanted.  If I can figure that out, I redirect to the "spelling corrected" page assumption.  If I cannot, I redirect to the home page or the site map.  

This is a simplified example that allows you to "forget" the .php suffix and still get to the right page.  You can see it in action here:
www.landonbaseball.com/coaches
<?php // 404page.php
error_reporting(E_ALL);
session_start();

if (isset($_SERVER["REQUEST_URI"]))
{
    // SAVE THE REQUEST
    $_SESSION["REQUEST_URI"] = $_SERVER["REQUEST_URI"];

    // IF A PHP SCRIPT IS NOT FOUND GO TO THE HOME PAGE
    if (preg_match('#\.php#i', $_SERVER["REQUEST_URI"]))
    {
        header('Location: /');
        exit;
    }

    // IF A NON-PHP SCRIPT IS NOT FOUND, TRY IT WITH PHP
    $arr = explode('?', $_SERVER["REQUEST_URI"]);
    $arr[0] .= '.php';
    $uri = implode('?', $arr);
    header("Location: $uri");
    exit;
}

// IF NO REQUEST URI
header('Location: /');
exit;

Open in new window

Author

Commented:
Thanks for your help and feedback,
I have a content management system and would like to make it easier for users to add text links from one page to another instead of creating links to a page using the page ID stored in database.

I would like to use
<a href="Online Shop">Online_Shop</a>
instead of
<a href="index.php?page_id=154">Online Shop</a>

I was therefore thinking of running a PHP script every time a page is added or changed via the CMS and match the page name with the page id, then update my .htaccess file with the new Rewrite Rules.  

RewriteRule Online Shop index.php?page_id=154
RewriteRule Gallery index.php?page_id=42

It looks like it will work except for a problem with spaces (or more than one word in page name), so above links to “Gallery” page works but not to “Online Shop”

Not sure if it’s better to modify the .htaccess file or use a separate page as mentioned by Ray?
You are right about impossibility of having url with space in htaccess.
In terms of SEO both offered ways are equivalent.
Most Valuable Expert 2011
Top Expert 2016
Commented:
You might get away with something that is URLencoded like RewriteRule Online+Shop index.php?page_id=154

But a better design pattern might be to allow users to "tag" pages with keywords.  The general design works like this:

1. Each page has its own set of rows in your data base.
2. You have a form on the bottom or right side of the page that carries a prompt "Add your tags to this page."
3. When the form is submitted a row is added to the data base containing the client-generated tag.
4. Simultaneously, an email is generated to the moderator (probably you) showing the tag and a link to the script that allows you to approve, edit, or delete the tag.
5. After your action, the row is either deleted or marked permissible for use.
6. As the HTML for the page is generated, the <meta keywords> are drawn from the tags in the data base.
7. A tag "cloud" can be generated and added to the page, if you want.
8. A Captcha test is a good thing to have as a supplement to the form input.

Over time this strategy will have a wiki effect, allowing your population of site visitors to give voice to the way they interpret the meaning of your pages.

Author

Commented:
Thanks you for your help, I will have a think about which way to go, that is using .htaccess file or a seperate "missing page" handler.

Also, as an FYI, another member mentioned that you can escape the space with backslash in the .htaccess file which works great.
RewriteRule Online\ Shop index.php?page_id=154