Change displayed URL using htaccess

5.7k Views Asked by At

How could I rewrite my URL using HTACCESS?

When a visitor visits index.php, I want the URL to be rewritten to something else, although the visitor will remain on the index.php page.

This is what I've tried (I did research this before asking but couldn't solve it myself):

RewriteEngine on
RewriteRule ^index.php$ testingit.php

Basically, I just wanted to change index.php to 'testingit.php', just to see if it would work.

2

There are 2 best solutions below

5
anubhava On BEST ANSWER

You can use this code in your testwebsite/.htaccess file:

RewriteEngine On
RewriteBase /testwebsite/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^ home [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^home/?$ index.php [L,NC]
13
Admin Alex On

So here's how you do it:

redirect *** /index.php http://www.yoursite.com/testingit.php

You need to replace *** with one of the following:

  • 301-For a permanent redirect meaning browsers update bookmarks etc.
  • or
  • 302-For a temporary redirect
  • Here's a link to a guide I found:

    https://www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/

    Hope this helps :)

    To make pretty URL's:

    RewriteEngine On    # Turn on the rewriting engine
    RewriteRule    ^home/?$    index.php    [NC,L]