0

Let's say I have a website at www.hostname.com/example. I would like to create a page, that generates based off of any extension of the original url. For example, I would have a page called setup.php. If someone were to go to www.hostname.com/example/xyz, it would load setup.php, and utilize xyz as a variable to customize setup.php. Is this possible? I have tried searching google, but have found no results. Any thoughts?

5
  • 1
    Here you need to use .htaccess, neither php nor javascript. Commented Apr 24, 2014 at 16:36
  • Could you elaborate a bit? Is it just a boolean value I need to change? Commented Apr 24, 2014 at 16:36
  • No, it is a special file where you place commands to the server. Search rewrite rule htaccess in google to find it out. Commented Apr 24, 2014 at 16:38
  • This should introduce you to what you need to do: httpd.apache.org/docs/2.0/misc/rewriteguide.html Commented Apr 24, 2014 at 16:39
  • Here is a good explanation for beginners: askapache.com/htaccess/mod_rewrite-basic-examples.html Commented Apr 24, 2014 at 16:41

2 Answers 2

1

Make a file called .htaccess and place it in the main directory. Then put this code in it:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ setup.php?data=$1 [QSA,L]

Now whatever url you open, the file setup.php will be executed and you can get the requested data with the $_GET['data'] variable. For example:

<?php
$data = $_GET['data'];
if($data === 'xyz') {
echo "<b>xyz</b> is requested!";
include "xyz.php";
}
else {
echo "error 404: page not found";
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is right, except I want the data after the first folder. So if I was at servername.com/folder/xyz, it would return xyz to the setup.php file.
1

Take for example the url http://www.noticeeverything.com/news/. You could grab just the string "news" like this:

var url = window.location.href; 
var str = url.substring(32, 36); 
console.log(str);

Or, without having to determine the index of the substring you want:

var url = window.location.href; 
var str = url.replace('http://www.noticeeverything.com/news/', 'news'); 
console.log(str);

Or, if you don't know what the url is specifically, but expect it to follow the same pattern (i.e. 'http://www.something.com/something/'):

var pattern = /[a-z]+/; 
var url = window.location.href;
var newString = url.replace("http://www.", '').replace(pattern, '').replace('.com/', '').replace('/', '');
console.log(newString);

Hope that helps.

Comments

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.