Reputation: 95
I am in need of help with the damn htaccess file, RegEx just confuses me I can handle the basic redirects but once it gets into query strings I lose it.
So here is the problem
I am trying to clean up my urls so I want to take the original path:
http://localhost/webcampaign/projects/project.php?id=1917-Bungalow
and have it show up has
http://localhost/webcampaign/projects/1917-Bungalow
this is a photo of file structure to give a better idea
I have all projects in an array set up like this
$projects = array();
$projects["1917-Bungalow"] = array(
"name" => "1917 Bungalow",
"heroClass" => "hero-bungalow",
"mainImg" => BASE_URL ."images/bungalow_main.jpg",
"previewImg" => BASE_URL ."images/bungalow_preview.jpg",
"year" => "2013",
"yearClass" => "twenty13",
"location" => "Autstin, Texas",
"photographer" => "Rachel Cruz",
"detailOne" => BASE_URL ."images/bungalow_detail1.jpg",
"detailTwo" => BASE_URL ."images/bungalow_detail2.jpg",
"detailThree" => BASE_URL ."images/bungalow_detail3.jpg",
"detailFour" => BASE_URL ."images/bungalow_detail4.jpg",
"description" => "lorem ipsum and shit",
"detailOneSum" => "lorem ipsum",
"detailTwoSum" => "lorem ipsum",
"detailThreeSum" => "lorem ipsum",
"detailFourSum" => "Lorem ipsum",
"materials" => array("matOne","matTwo","matThree"),
"materialImg" => array(BASE_URL ."images/bungalow_mat1.jpg",BASE_URL . "images/bungalow_mat2.jpg",BASE_URL ."images/bungalow_mat3.jpg"),
"testamonialImg" => BASE_URL ."images/client.jpg",
"testamonial" => "Cabin Is Great!"
);
And then in the project.php page I access the individual projects with
$project_id = $_GET["id"];
$project = $projects[$project_id];
Would that effect anything? thanks I am a total newb at this
This was my last attempt with the Rewrite
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^project.php$ /projects/%1/? [R=301]
Any help with this would be greatly appreciated. Cheers
Upvotes: 0
Views: 357
Reputation: 4784
The ^
character matches the beginning of the path but the path in the URL starts with webcampaign
not project.php
.
Try dropping the ^
from your RewriteRule
. I also think you need to drop the /projects/
from the redirect URL since it looks like that is the directory the .htaccess resides in.
RewriteEngine On
RewriteBase /webcampaign/
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule project.php$ projects/%1? [R=301]
EDIT:
So based on your updated question it seems you want the inverse of what was requested as the rewrite module only rewrites incoming URLs, not those that your script outputs.
First thing is in your HTML output to change your links to the format that is to be displayed. This is what the commenter meant by "adjusting the links on the html pages".
echo '<a href="http://localhost/webcampaign/projects/1917-Bungalow">1917 Bungalow</a>';
Next use these new .htaccess
rules to rewrite the above URL to your project.php page with the correct id
parameter:
RewriteEngine On
RewriteBase /webcampaign/
RewriteRule projects/([A-Za-z0-9-]+)/?$ projects/project.php?id=$1 [NC,L]
Upvotes: 1
Reputation: 785128
Put this code in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(webcampaign/projects)/project\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(webcampaign/projects)/([^/]+)/?$ $1?id=$2 [L,QSA,NC]
Upvotes: 0