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>". substr($row['company'], 0,26)."</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
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>". substr($row['company'], 0,26)."</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.
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.
Related
I am using an .htaccess file to redirect "http://www.domain.com/cars/cars.php?cars_item=231" to "http://www.domain.com/cars/231/porsche"
So basically I'm redirecting 'cars.php?cars_item=231' to '$id/$title'
The .htaccess file consists of:
#Start
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect cars.php?cars_item=231 to cars/231/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+cars/cars\.php\?cars_item=([^&\s]+) [NC]
RewriteRule ^ /cars/%1 [R=302,L]
# Internally forward cars/231/ to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+) /cars/cars.php?cars_item=$1 [NC,L,QSA]
#end
Then i'm using the following hrefs:
"http://domain.com/index.php" = echo porsche;
and in "http://www.domain.com/cars/cars.php" i'm using a $_GET['cars_item'];
My question is: How could I remove the $id from the url please? I want to have the following url as a result - "http://www.domain.com/cars/porsche" ... so basically I want to update the .htaccess and redirect "http://www.domain.com/cars/cars.php?cars_item=231" to "http://www.domain.com/cars/porsche"
This is because the $id in the url is creating some problems for indexing especially. Google keeps crawling "http://www.domain.com/cars/$id/" where as the $id is not a folder but is a db row hence I'd like to remove the $id from the url.
I would appreciate your help as always.
I have my query string to include my pages, in my homepage like you see below and it is working fine, Im including my pages fine.
But something wrong is happening and Im not finding how I can solve this.
I will try to expain my isse with an example: I have a folder "teachers" inside I have two pdf documents and a page "documents.php".
To acess this documents page, Im acessing: "htp://localhost/website/teachers/documents", and it is working fine.
But If I acess "htp://localhost/website/teachers/", Im able to acess my pdf documents and my page as you see in my image below.
But I dont want this, I want that If some user tries to acess "htp://localhost/website/teachers/", I want to include my 404 file (require_once('inc/404.php');)
My query string:
#$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);
if(file_exists('inc/'.$url[0].'.php')){
require_once('inc/'.$url[0].'.php');
}
elseif(file_exists($url[0].'/'.$url[1].'/'.$url[2].'.php')){
require_once($url[0].'/'.$url[1].'/'.$url[2].'.php');
}
elseif(#file_exists($url[0].'/'.$url[1].'.php')){
require_once($url[0].'/'.$url[1].'.php');
}
else{
require_once('inc/404.php');
}
Do you see what Im doing wrong to not be having the result I want?
My htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
There is an easier solution.
Add this line to the beginning of your .htaccess file:
Options -Indexes
This way, you won't be able to see the folder contents.
EDIT: htaccess rule solution (which is in website folder)
ErrorDocument 404 /website/inc/404.php
RewriteEngine On
RewriteBase /website/
RewriteRule ^teachers/$ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
I have URLs like
book.php?id=23 product.php?id=23 etc..
I want them to be something like
book/id/title
my current .htaccess is this
RewriteEngine On
RewriteRule ^book/([0-9]+)\-([a-z0-9_\-]+)/?$ book.php?id=$1 [NC,L,QSA]
but it's not working properly,
it's directing to book.php page despite of problems in page, I can't Get the Id parameter using $_GET , any help is appreciated
here's my url http://www.gamzesart.com/book.php?id=2
Actually I had this problem the other week, it turned out it was because the url reference was the same as the name as the php file I was calling, i.e:
RewriteRule ^book/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
Just as a test, change your rule to:
RewriteRule ^books/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
and try calling the URL as books/23 instead.
Try the following:
+FollowSymLinks # This is based on the comment of PeteR bellow in comments
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^book/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
</IfModule>
That works for me.
In my local PC I have try to access the URL : http://www.localSite.dch/book/12/nikos
and in my book.php file I have the following code:
<?php
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
The result is this:
Array
(
[id] => 12
)
I'm working on a URL shortener script.
My script generates a link like http://127.0.0.1:1337/urlshortener/v5tjp.
v5tjp is a random value, generated by a script.
My script's logic is that I input an URL, then PHP takes it, generates a random value (with the length taken also from the SQL database), then inserts the long url and the short url in the database.
Where I'm stuck: I need to create a .htaccess file to redirect the visitor to redirect.php, where I have the redirect script.
This is the redirect.php file:
<?php
include ('connect.php');
$decode = mysql_real_escape_string($_GET['decode']);
$sql = 'SELECT * FROM urls WHERE short_code="$decode"';
$result = mysql_query($sql);
if (isset($_GET['url_token'])){
$urlId=$_GET['url_token'];
$query = "SELECT * FROM urls WHERE short_code=".$urlId." LIMIT 1";
$redirect = mysql_query($query);
if(mysql_num_rows($redirect)) {
$row = mysql_fetch_assoc($redirect);
$url = $row['long_url'];
header('Location: http://'.$url);
}
echo 'Bad URL!';
exit();
}
while($row = mysql_fetch_array($result))
{
$res=$row['long_url'];
header("location:".$res);
}
This is the .htaccess file I've made:
RewriteEngine On
RewriteRle ^$ index.php [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteRule ^(.*)$ redirect.php?url_token=$1 [L]
But for some reason it's not working. I'm running my script with XAMPP.
RewriteRule ^$ index.php [L]
You missed a 'u'.
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRle ^$ /index.php [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteRule ^urlshortener/(.*)$ /redirect.php?url_token=$1 [L,QSA,NC]
I want to redirect all to one script e.g. index.php?url=inputurl
With if/else I want to parse url
in index.php run query for url in my custom table
if url is mach: echo "ok"
else do nothing
How should I set .htaccess in root folder of Wordpress?
Example:
URLs in custom_table:
asd
dfg
ghj
If user puts:
www.mysite.com/asd
-> mod_rewrite should output this: www.mysite.com/index.php?url=asd
Else if user puts:
www.mysite.com/zzz
-> do nothing
I think the following .htaccess should solve the problem:
RewriteEngine On
# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]
Edit: to not include your folders and files (like /js, /css, etc.) in rewrite, add the following lines before the RewriteRule line (see comments):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And in the PHP script:
$url = $_GET['url'];
// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
// do something here
// maybe redirect with header('Location: path')
} else {
// show a not found page (error 404)
}
You want to both be able to read from a database and do nothing if there is not match.
This would require you run code to access db then return back to apache to process and is not possible from .htacccess (though it is from httpd.conf).
The .htaccess solution would be to specify all the "table" entries inline as below.
RewriteEngine on
RewriteBase /
#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]