Reputation:
I have a site in localhost that uses a shortened URL's from
http://localhost/Portal/mysite/profile.php?id=1
to
http://localhost/Portal/mysite/profile/1/this_is_id
Using the below .htaccess
below
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^profile/([0-9]+)/.*$ /Portal/mysite/profile.php?id=$1 [QSA,L,NC]
But, I need the URL to be instead as http://localhost/Portal/mysite/this_is_id
This is all in localhost, that is why .com
does not appear, but the site file is mysite
So, I tried sending a link that is in my mysite/index.php
to mysite/profile.php
with $id
but it is not working. Anyway simple suggestion would be fine, thanks
UPDATE
Ok, I did as both you asked, but this is the function that is sending a user from index page to profile page, and I do not know how to modify it, even though I have done what both of you asked.
// sql query goes here.
foreach ($stmt as $row) {
$url = "/profile/$row[id]/".preg_replace('/[^a-zA-Z0-9-_]/', '-', $row['company']);
echo "<h4><a href=\"$url\" class=\"urln\">". substr($row['company'], 0,26)."</a></h4>
";
}
the echo is the link, now when I press on that link, It takes me to page not found
UPDATE 2
this is the image of my directory, the link is in index.php and when clicked it sends me to profile.php all the above file is situated in
WAMP/www/Portal/mysite
Upvotes: 0
Views: 78
Reputation: 3088
Try this:
#for subdirectory
RewriteBase /Portal/mysite/
#for localhost
#RewriteBase /
#RewriteRule ^profile/([0-9]+)$ profile.php?id=$1 [QSA,L,NC]
#RewriteRule ^profile/([0-9]+)/([a-zA-Z0-9\-]+)$ profile.php?id=$1 [QSA,L,NC]
RewriteRule ^profile/([a-zA-Z0-9\-]+)$ profile.php?id=$1 [QSA,L,NC]
.htaccess
and profile.php
in Portal/Site
directory.
UPDATE:
foreach ($stmt as $row) {
$url = "/profile/".preg_replace('/[^a-zA-Z0-9-_]/', '-', $row['company']) . "-" . $row[id];
echo "<h4><a href=\"$url\" class=\"urln\">". substr($row['company'], 0,26)."</a></h4>";
}
profile.php
var_dump($_GET);
if(isset($_GET['id'])) {
$params = explode('-', $_GET['id']);
$id = (int)array_pop($params);
var_dump($id);
}
.htaccess
RewriteEngine On
RewriteBase /Portal/mysite/
RewriteRule ^profile/([a-zA-Z0-9\-]+)$ profile.php?id=$1 [QSA,L,NC]
run your link ( f.e.: localhost/Portal/mysite/profile/This-is-name-12
) and result:
array (size=1) 'id' => string 'This-is-name-12' (length=15)
int 12
12
is your profile id.
Upvotes: 1
Reputation: 4736
Try:
RewriteRule ^([0-9]+)$ profile.php?id=$1 [QSA,L,NC]
if the Rule (.htaccess
) is in your /Portal/mysite/
directory, and profile.php
is there as well.
Upvotes: 0