We help IT Professionals succeed at work.

.htaccess - redirect text links that don't exist to specific page

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

Top Expert 2005

Commented:
> is it possible to use .htaccess to redirect text links that don't exist to specific page.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /error_nonexitent.html [L]

> 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 
Not really sure if You after that(please elaborate how to guess if it's online shop or contact), but
RewriteEngine on
RewriteCond %{REQUEST_URI} ^online_shop
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /online_shop/main.html [L]

RewriteCond %{REQUEST_URI} ^contact
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /contact/main.html [L]

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”
Top Expert 2005
Commented:
Just escape the space with backslash
RewriteRule Online\ Shop index.php?page_id=154

Author

Commented:
Thanks ravenpl for your help, much appreciated.