@declan_ritchie
To manipulate $_GET
values in the URL using PHP and .htaccess, you can follow these steps:
- Enable the use of mod_rewrite in Apache by ensuring it is enabled. You can check this by looking for LoadModule rewrite_module modules/mod_rewrite.so in your Apache configuration file (httpd.conf). If it's commented out, remove the # at the beginning of the line to enable it.
- Create or edit the .htaccess file in the root directory of your website. If it doesn't exist, create a new file and name it .htaccess.
- Add the following lines of code to the .htaccess file to enable rewriting URLs:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Explanation of the code:
RewriteEngine On enables the RewriteEngine.
RewriteBase / sets the base directory for rewrite rules.
RewriteCond %{REQUEST_FILENAME} !-f ensures that if the requested file exists, the rewrite rules won't be applied.
RewriteCond %{REQUEST_FILENAME} !-d ensures that if the requested directory exists, the rewrite rules won't be applied.
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] redirects all requests to the index.php file and passes the original URL as a GET parameter called url. The [QSA] flag appends any existing query string, and the [L] flag stops further rule processing.
- In your PHP code (e.g., index.php), you can access the rewritten URL using the $_GET['url'] variable and manipulate it as needed. For example:
With these steps, you will be able to manipulate the $_GET
values in the URL using PHP and .htaccess.