John Davidson

.htaccess - Get the PATH in htaccess using $_GET in PHP

0 comments
Message:


Let's suppose that my site is example.com/pageone.


How can I rewrite my htaccess to get the pageone name using $_GET with PHP?


For example, in my code, I want to do something like this:


if($_GET['page'] === 'pageone' && file_exists('./pages/' . $_GET['page'] . '.php')) {
require_once './pages/' . $_GET['page'] . '.php';
} else {
require_once './pages/home.php';
}


My Answer:
You can achieve this by using the following code in your .htaccess file:

apache
RewriteEngine On
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]


This code will rewrite the URL from example.com/pageone to example.com/index.php?page=pageone. Then, in your PHP code, you can access the 'page' parameter using $_GET['page']:

php
if(isset($_GET['page']) && $_GET['page'] === 'pageone') {
// Your code here
}


This way, you can check if the 'page' parameter is set to 'pageone' and perform the desired actions accordingly.

Rate this post

4 of 5 based on 9045 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us