php - clean URL
See URL rewriting in PHP without .htaccess
if you don't want to or can't use .htaccess
, else refer to How to: URL rewriting in PHP?.
How to apply php function to $_GET to get clean URLs?
I finally fixed it by working around the code.
Here is the new htaccess code making first parameter optional and picking ID as the main parameter to work properly:-
RewriteRule ^(.*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]
Changes done on locality.phpMinor changes are done to use ID to fetch the rows in the following manner :-
<?php
include($_SERVER['DOCUMENT_ROOT'].'/database-path.php');
$localID = base64_decode($_GET['id']) ;
$sql="SELECT * FROM table_name WHERE id='$localID'";
$result = mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($result);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$locality = $row['locality'];
$content = $row['content'];
$district = $row['district'];
$state = $row['state'];
$iso = $row['fld_iso'];
}
}
?>
Thank you everyone for your help. How to clean url when using get method
rubberholds/product
- Test hererubberholds/product/product-one
- Test here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^rubberholds/product/?(.*)$ rubberholds/product.php?item=$1 [QSA]
Test data :
$limestoneItems = [
'item1' => [
'title' => 'Product-One',
'img' => 'product1',
'alt' => 'Product1',
],
'item2' => [
'title' => 'Product-Two',
'img' => 'product2',
'alt' => 'Product2',
],
'item3' => [
'title' => 'Product-Three',
'img' => 'product3',
'alt' => 'Product3',
],
];
product.php$title = strtolower($_GET['item']);
$data = array_filter($limestoneItems, function ($v) use ($title) {
$v['title'] = strtolower($v['title']);
return in_array($title, $v);
});
if ($data) {
$key = array_keys($data)[0];
$product = $limestoneItems[$key];
var_dump($product); // Data found
} else {
var_dump('Not found'); // Data not found
}
mod_rewrite Clean URL rewriting
You can combine two line in one line:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1
In this case /
will be optional Error in htaccess file when clean url of php file
It's this rule (that appends theRewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
.php
extension via an internal rewrite) that is incorrect.This results in a rewrite-loop when requesting /single-portfolio/<anything>
because /single-portfolio.php
exists (satisfying the condition) but this rule will incorrectly rewrite the request to /single-portfolio/<anything>.php
, which would ordinarily result in an erroneous 404 (since you are expecting the last rule to catch this request), but due to the L
flag the rewriting engine starts over and repeatedly rewrites the request to /single-portfolio/<anything>.php.php
to /single-portfolio/<anything>.php.php.php
etc. etc. until the server "breaks" and a 500 response is returned.
See my answer to the following question on ServerFault with a more detailed explanation of this behaviour: https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
Simply changing L
to END
will fix the rewrite-loop, but won't resolve the overall problem because you'll just get the erroneous 404 as mentioned above (the request won't be correctly rewritten).
Changing the order of your directives so the last rule that rewrites ^single-portfolio/
is before your second rule that appends the .php
extension (via an internal rewrite) would solve your immediate problem, however, you need to fix the above rule so you are testing the same file you are ultimately rewriting to (otherwise, any URL of the form /php-file-that-exists/<anything>
would result in a rewrite-loop / 500 Internal Server Error response, instead of the expected 404).
For example:
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R]
RewriteRule ^single-portfolio/(\d+)/([\w-]+)$ single-portfolio.php?id=$1&title=$2 [NC,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
NB: The shorthand character class \w
is the same as [0-9a-zA-Z_]. And
\dis the same as
[0-9]`.The NC
flag on the first and last rules are entirely superfluous. I would be wary of using the NC
flag on the ^single-portfolio/
rewrite (it shouldn't be necessary), as this potentially enables duplicate content (eg. /single-portfolio/...
and /SiNgLe-PoRtFoLiO/...
are both valid and resolve to the same resource.)
The first rule that removes the .php
extension via an external redirect should ultimately be a 301 (permanent) redirect once you have confirmed everything is working OK.
Related Topics
Print a Webpage to PDF Document Using PHP
How to Convert Ldap Timestamp to Unix Timestamp
How to Disable the Back Browser Button After User Press Logout and Destroy Session
Check and Return Duplicates Array PHP
MySQL and Query to Satisfy on Same Column
When Should I Close a Database Connection in PHP
How to Add Item to the JSON File Formatted Array
How to Get the Session Id in Laravel
Png Transparency Resize with Simpleimage.PHP Class
Composer:How to Add a Dependency Without Network Connection
PHP Resize Image on or Before Upload
Usort Issue with Decimal Numbers
How to Skip Elements in Foreach Loop
How to Retrieve Date from Table Cell Using PHPspreadsheet