1

If my URL is http://www.xyz.com/index.php?view=abc then it should open http://www.xyz.com/abc.php

What should I write in .htaccess??

Thanks in advance.

2
  • 1
    Which do you want your end users to type into their browsers? index.php?view=abc (which is unusual from normal rewriting), or abc.php? The way you asked doesn't make it clear which your users will see. Commented Oct 8, 2011 at 13:06
  • actually I want if user will open index.php?view=abc then abc.php will be execute. and if user enters directly abc.php then he/she can not open that page. User must need to access it via index.php?view=abc. Commented Oct 11, 2011 at 14:32

1 Answer 1

2

If my URL is http://www.xyz.com/index.php?view=abc then it should open http://www.xyz.com/abc.php

You don't need .htaccess for that, simply put in index.php

$view = $_GET['view'];
if (strpos($view, '/') === false && $view != "index" && file_exists("$view.php"))
  require "$view.php";

Be sure not to have any other vulnerable php files in your root directory (but I'd recommend using other/better techniques to check the user input from the url).


If you wanted to have the rewriting the other way round, your .htaccess could look similar to this:

RewriteEngine on

RewriteCond %{REQUEST_URI}  !^/index.php
RewriteRule ^([a-z]+).php$  /index.php?view=$1  [L]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Czechnology. It works. but what if I need reverse case. if some one types URL like, www.xyz.com/login.php then it should be redirect to, www.xyz.com/index.php?view=login
@user985361, isn't that exactly what's in the second part of my answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.