I’m having an issue using the .htaccess file to create a vanity URL.
My .htaccess file is currently saved in my site’s root folder (mysite/.htaccess). The page that I am writing the vanity URL for is one level down (mysite/jobs/job.php).
The code in my .htaccess file is:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/jobs/job.php?job_id=$1
Within the mysite/jobs/job.php file, I have included the following code:
if(isset($_GET['job_id']) == true && empty($_GET['job_id']) == false) {
$job_id = $_GET['job_id'];
echo $job_id;
}
My problem is this: when I type in "http://localhost/mysite/jobs/1" into my browser, this echoes jobs/1, when I want it to echo 1 (i.e. the actual job id without “jobs/” at the beginning). Does anyone know how I can change the .htaccess file to achieve this without having to move the php file up one level?
That's because your RewriteRule is capturing the entire REQUEST_URI (because of your regular expression). Try the following instead:
RewriteRule ^jobs/(\d+)$ /mysite/jobs/job.php?job_id=$1
(Can't remember if the leading slash needs to be there or not...try it both ways).