URL Modification with GET Parameters

I’m getting all kinds of confused now… why is there no domain.tld/account_id/{id} in your tests anymore?

  1. What URL do you want users to enter?
  2. Should the browser stay at that URL or redirect somewhere else?
  3. Which file should actually be executed at the final URL?

Ok, I totally confused myself before as well, so let me just rewrite this totally.


HTACCESS EXAMPLE 1 (MULTI VIEWS ON)

IndexIgnore * 
Options -Indexes

Options +MultiViews +FollowSymLinks

RewriteEngine On

RewriteBase /

#Remove .html and .php extension (THIS WORKS)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]


#Attempt no.1
RewriteRule ^([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.2
RewriteRule ^([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.3
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.4
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.5
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.6
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.7
RewriteRule ^account_id/(.*)$ ./viewaccount.php?account_id=$1
#Attempt no.8
RewriteRule ^account_id/(.*)$ ./FOLDER-NAME/viewaccount.php?account_id=$1
#Attempt no.9
RewriteRule ^account_id/(.*)$ viewaccount.php?account_id=$1
#Attempt no.10
RewriteRule ^FOLDER-NAME/account_id/(.*)$ ./viewaccount.php?account_id=$1
#Attempt no.11
RewriteRule ^FOLDER-NAME/account_id/(.*)$ ./viewaccount?account_id=$1
#Attempt no.12
RewriteRule ^account_id/(.*)$ ./viewaccount?account_id=$1
#Attempt no.13
RewriteRule ^account_id/(.*)$ ./FOLDER-NAME/viewaccount?account_id=$1

HTACCESS EXAMPLE 2 (MULTI VIEWS OFF)

IndexIgnore * 
Options -Indexes

Options -MultiViews +FollowSymLinks

RewriteEngine On

RewriteBase /

#Remove .html and .php extension (THIS WORKS)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]


#Attempt no.1
RewriteRule ^([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.2
RewriteRule ^([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.3
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.4
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.5
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.6
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.7
RewriteRule ^account_id/(.*)$ ./viewaccount.php?account_id=$1
#Attempt no.8
RewriteRule ^account_id/(.*)$ ./FOLDER-NAME/viewaccount.php?account_id=$1
#Attempt no.9
RewriteRule ^account_id/(.*)$ viewaccount.php?account_id=$1
#Attempt no.10
RewriteRule ^FOLDER-NAME/account_id/(.*)$ ./viewaccount.php?account_id=$1
#Attempt no.11
RewriteRule ^FOLDER-NAME/account_id/(.*)$ ./viewaccount?account_id=$1
#Attempt no.12
RewriteRule ^account_id/(.*)$ ./viewaccount?account_id=$1
#Attempt no.13
RewriteRule ^account_id/(.*)$ ./FOLDER-NAME/viewaccount?account_id=$1

Code in /FOLDER-NAME/viewaccount.php file

<?
echo "hello there!";
echo $_GET['account_id'];
?>

MULTI VIEWS OFF

URL Entered URL Loaded Code Responce
domain.tld/FOLDER-NAME/viewaccount.php?account_id=id_52213234 404 ERROR (URL entered returned a HTTP status code of 302 Found, then redirected to 404 error according to dev tools)

MULTI VIEWS ENABLED

URL Entered URL Loaded Code Responce
domain.tld/FOLDER-NAME/viewaccount.php?account_id=id_52213234 domain.tld/FOLDER-NAME/viewaccount?account_id=id_52213234 hello there!id_522132

I think what is happening is that it is trying to rewrite domain.tld/FOLDER-NAME/viewaccount.php?account_id=id_52213234 to domain.tld/FOLDER-NAME/viewaccount.php/account_id/id_52213234, a folder structure that does not exist. So basically, how can the .htaccess code be changes to only visually change the URL for the visitor, and not affect the backend at all.

I don’t understand. That server doesn’t seem to be behaving the way I would expect…

Can you try the PHP file without any .htaccess configuration at all?

Sure! Renamed it to htaccess.txt, results below

URL Code Response
domain.tld/FOLDER-NAME/viewaccount.php?account_id=id_522132 hello there!id_522132
domain.tld/FOLDER-NAME/viewaccount.php hello there

I really do not understand this. Does someone have a server they are willing to test this code on?

Here we go:

<?php declare(strict_types=1);

$title = 'https://Na.anetizer.com Testing Pretty URLs :)';

$baseURL  = 'https://' .$_SERVER['HTTP_HOST'] .'/pretty-urls';
$pInfo    = $_SERVER["PATH_INFO"] ?? '??? PATH_INFO NOT SET ???';
$aInfo    = explode('/', $pInfo);
$sInfo    = print_r($aInfo, TRUE);
$aServer  = print_r($_SERVER, TRUE);
$htaccess = print_r(file_get_contents('.htaccess'), TRUE);

echo $tmp = <<< ____EOT
<!doctype html><html lang="en-GB">
<head>
<title> $title </title>
<style>
dl {
	width:88%; margin:0 auto; 
	border:solid 1px #ddd; 
	padding:0.42rem
}
dl dt {
	font-size:large;
	font-weight:700;
}
dl dd {
	margin-bottom: 1rem;
}
</style>
</head>
____EOT;
echo '<body>';

echo '<h1>' .$title .'</h1>';
echo '<hr><hr>';


echo $tmp = <<< ____EOT
 <dl>
	<dt> Test OLD link </dt>
	<dd> 
		<a href="{$baseURL}?account_id=id_10464141"> {$baseURL}?account_id=id_10464141 </a>
	</dd>
	<dt>Test NEW link </dt>
	<dd> 
		<a href="{$baseURL}/index.php/account_id/id_10464141"> {$baseURL}/index.php/account_id/id_10464141  </a>
	</dd>
	<dt>Test NEW link - WITHOUT index.php </dt>
	<dd> 
		<a href="{$baseURL}/account_id/id_10464141"> {$baseURL}/account_id/id_10464141  </a>
	</dd>

	<dt> <i>Results:</i> \$_SERVER["REQUEST_URI"] </dt>
	<dd> 
		{$_SERVER['REQUEST_URI']} 
	</dd>
	
	<dt> <i>Results:</i> \$_SERVER["PATH_INFO"] </dt>
	<dd> 
		$pInfo
		<br> 
		<pre> $sInfo </pre> 
	</dd>
	
</dl>

	<hr>
	<h2> print_r('.htaccess') </h2>
	<p>
		<pre> $htaccess</pre>
	</p>

	<h2> print_r(\$_SERVER) </h2>
	<div>
		<b> <b> REMOVE leading slash to show results:</b> \$aServer  
		<pre> \$aServer </pre>
	</div>

</body>
</html>
____EOT;

Live Link

Try clicking the three different links and notice the differences returned with:

Results: $_SERVER["REQUEST_URI"]
   
Results: $_SERVER["PATH_INFO"] 

This is what you want, isn’t it?

So that code is allowing both to be used, but how can I force the modified link to always be shown?

So this link type is always displayed (So the URL is always converted).
domain.tld/pretty-urls/account_id/id_10464141

However, I still need to be able to use $_GET['account_id'] to get id_10464141

That is for you application to change. Apache RewriteEngine will only make sure that when such a URL is visited then the correct file is loaded.

Showing the correct URLs is up to you.

1 Like

Oh. So that code that I had originally probably did work, I just did not test it correctly. Got it. So it is not possible to do it automatically with htaccess, I will have to edit my PHP.

Thanks for your help!

Did you try uploading the index.php and .htaccess file to your site? The files are configured to work in a subdirectory. Did you experiment with different URLs and notice the Results? Also did you enable viewing the $_SERVER parameters?

The s_GET array is always available but could be an empty array, unlike s_POST which may be available. Activating and showing the $_SERVER parameters is a useful debugging technique.

Ensuring index.php is always called and testing the URL parameters allows the index.php to:

  1. require “getpage.php”; and return with a **$page” which can then include/require “$page”; The $page maybe “page-not-found.php “:

  2. The index.php can also include a header.php and footer.php simplifying page layout changes.

  3. The $page can later be transferred to a database table

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.