I am very confused so I decided to buy support from the Stack Overflow community.
I have a URL shortening service with no database that stores the URLs in the / urls directory and can be accessed as site.com/url, without having to write site.com/urls/url.php and removing the PHP file extension. Which means that when you write site.com/url, you are doing a rewrite of site.com/index.php?url=$1, which in my PHP code includes the file, checking first whether it exists or not.
That is already ready and functional. I use the following code in my .htaccess:
RewriteEngine On
RewriteRule ^(\w+)/?$ index.php?url=$1.php
And I also use the following PHP code in the index.php file so that the rewrite can be carried out correctly:
if($_GET['url']){
$requestedshortenedurl = $_GET['url'];
$urlpath = "urls/$requestedshortenedurl";
if (file_exists($urlpath))
{
echo "";
}
else
{
header("Location: sitio.com?error=4");
exit;
}
include ("urls/$requestedshortenedurl");
exit;
} else {
echo "";
}
Now I was looking for how to make site.com?stats=url&visits and site.com?stats=url&visitors can be written as site.com/stats/url/visits and site.com/stats/url/ visitors.
I am using the following code in my .htaccess which is not working:
# Rewrite for shortened URLs visits
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^stats/([^/]+)/([^/]+)/?$ index.php?stats=$1.txt&visits [L]
# Rewrite for shortened URLs visitors
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^stats/([^/]+)/([^/]+)/?$ index.php?stats=$1.txt&visitors [L]
And I'm using the following code in my index.php file:
// Define variable stats
$stats = $_GET['stats'];
// Switch stats cases (visits or visitors)
if($_GET['stats'])
switch($_GET['stats']) {
case 'visits' :
if (file_exists($statsvisits))
{
include ("stats/$statsvisits");
}
else
{
header("Location: sitio.com?error=4");
exit;
}
break;
case 'visitors' :
if (file_exists($statsvisitors))
{
include ("stats/$statsvisitors");
}
else
{
header("Location: sitio.com?error=4");
exit;
}
break;
default :
header("Location: sitio.com?error=4");
break;
}
Which is not working and it takes me to sitio.com? error = 4 (404 error page).
The file I'm testing is stpe-visits.txt, which is located at site.com/stats/stpe-visits.txt. When writing site.com/urls/stpe-visits, it does not work.
Thank you.