URL ReWrite Affecting Page Content - php

I have a website with the following folder structure:
website >> magazine >> news
Inside news my files include:
htaccess, updates.php, articles.php & article.php
So my htaccess looks like:
Options -MultiViews
DirectoryIndex updates.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^updates updates.php
RewriteRule ^articles articles.php
RewriteRule ^updates/([\w-]+)/?$ updates.php?id=$1 [NC,L,QSA]
RewriteRule ^articles/([0-9]+)/?$ articles.php?currentpage=$1 [NC,L,QSA]
RewriteRule ^article/([0-9]+)/([\w-]+)/?$ article.php?id=$1&title=$2 [NC,L,QSA]
The problem(s) that I have is that when I visit website.uk/magazine/news/updates/1459967836 it shows the result for my article with ID 1460544406.
The code in my updates page is:
...
if(isset($_GET["id"])){$id = $_GET["id"];}else{
$id = "latest";
}
?>
<?php
if ($id == "latest"){$var = "ORDER BY added DESC LIMIT 1";}else{$var = "WHERE id = '$id'";}
?>
<?php
$posts_sql = "SELECT * FROM magazine_news_updates $var";
...
Does anyone know why when I visit 1459967836 I gewt shown the result for 1460544406.
Also, when I visit website.uk/magazine/news/article-add-form.php I get shown website.uk/magazine/news/articles, even though the URL is displayed correctly. Any Ideas?

** SOLVED **
By removing (
RewriteRule ^updates updates.php
RewriteRule ^articles articles.php
) the pages now show everything correctly!

Related

How to remove index.php and picture.php from Piwigo?

https://example.com/index.php/categories
On url index.php it does not look good. How can I remove index.php from url?
https://example.com/picture.php?/246/category/red-color-women-shoes
"picture.php?" - want to remove for friendly url
/246/ - Not sure what it is.
/category/ - I think it should be category name but shows "category"
Piwigo is open source photo gallery software for the web
https://piwigo.org/
https://example.com/index.php/categories
https://example.com/picture.php?/246/category/red-color-women-shoes
$conf['question_mark_in_urls'] = false;
$conf['php_extension_in_urls'] = false; // this does not work
$conf['category_url_style'] = 'id-name';
$conf['picture_url_style'] = 'file';
Url should be
https://example.com/categories
and
https://example.com/category-name/red-color-women-shoes
#TURN ON REWRITES
RewriteEngine on
RewriteBase /
#THEN OPTIONALLY MAKE IT www if they didnt enter it
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
#Write index.any to /
RewriteRule ^index\.[a-z]{2,3} / [L,R=301]
#THEN REWRITE LOCATIONS
RewriteRule ^([0-9]+)/category/(.*)$ picture.php?id=$1&page_url=$2 [L]
#IF CATEGORY IS ALSO DYNAMIC THEN TRY THIS FOR REWRITE
RewriteRule ^([0-9]+)/(.*)/(.*)$ picture.php?id=$1&category=$2&page_url=$3 [L]
If I understand your question right.. hope that helps

When I host my website I can acess homepage but I always get 404 error when I try to acess my secondary pages [duplicate]

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]

Rewrite URLs for SEO friendly with .htaccess on Wordpress

I'm developing a website using Wordpress (for articles, static pages, ...) but in my theme folder I created a subsystem (handled via php-mysql that doesn't require Wordpress) that uses another database (that contains products). The php page of a single product (product.php) is this:
<?php
...
if($_GET['url']){
$url = $_GET['url'];
$sql = "select * from `products` where url='$url'";
$row = // start the query ($sql);
$title=$row[0]['title'];
$body=$row[0]['body'];
}
else{echo '404 Page.';}
?>
<body>
<?php
echo "<h1>$title</h1>
<p>$body</p>";
?>
</body>
And it's rewritten by Wordpress to product/ in the URL, infact, opening (i.e.) this URL it works:
http://localhost:1234/my-site/product/?url=product-number-one
^
product.php
but I need to rewrite it to this
http://localhost:1234/my-site/product/product-number-one
.htaccess on Wordpress is by default this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my-site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my-site/index.php [L]
</IfModule>
# END WordPress
I added to that this rule:
RewriteRule ^(.*) product?url=$1 [R]
but doesn't work. I tried others solutions but nothing done.
What you can do is very simple:
RewriteRule ^my-site/product/([A-Za-z0-9-]+)$ /my-site/filename_of_product_page.php?url=$1 [L]
With the expression listed within the parentheses, you would need to be sure your url itself matches this- so use a scrubber function when inserting the name into the database.

Pretty urls with Htaccess show product name in the url but call the product with the id

I trying to do with the htaccess, something like that.
http://www.domain.com/product/fussball-in-munich/
But the link of the menu navigation will be like this,
Fussball
I'll call to the product by the ID, but i want to display in the url the product name,
I make this with my htaccess but doesnt work how i want.
RewriteEngine On
RewriteBase /dev
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^angebot/([A-Za-z-\s0-9]+)$ /dev/product.php?product_id=$1 [NC]
Options +FollowSymlinks -MultiViews
RewriteRule ^(.+)(\s|%20)(.+)$ /$1-$3 [R=301,QSA,L,NE]
My php file
if (isset($_GET['product_id'])){
$product_id = strip_tags($_GET['product_id']);
getProductsById($product_id);
}
the Function
$query = "SELECT * FROM `product` WHERE id='$product_id'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$count = $result->num_rows;
if ($count === 1){
echo "<div class='container mt50'>";
while($row = $result->fetch_assoc()){
echo '<strong>This Product has been found in the database </strong>' .$row['product_name'];
}
echo "</div>";
}else{}
Your htaccess file doesn't know anything about the relationship between a product id or its name, that's all in your DB. You'll have to code all of this up in your php file.
in the htaccess file, you can only have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^angebot/([A-Za-z-\s0-9]+)$ /dev/product.php?product_name=$1 [L]
then all the rest of the logic has to be coded into php:
check if "product_name" is set
check if $_SERVER['REQUEST_URL'] has product.php in it
strip out the stuff that made the name pretty (replace "-" with spaces)
lookup in the db for the product with the given name and get the ID
get the ID and call getProductsById($product_id)
Then the other way around (redirect the browser if they access the ugly URL):
check if "product_id" is set
check if $_SERVER['REQUEST_URL'] has product.php in it
if it does, someone requested directly your product ID
use the ID, do a db lookup for the product name, clean up the product name so that it doesn't look ugly (maybe spaces get changed to "-")
redirect the browser to the new pretty URL

htaccess for shortened URL

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]

Categories