I recently had the following answered: .htaccess rewrite url does not work
I would like to now add the title from the db for each id to the URL too. From what I've read on the internet it seems that it is possible. Mostly I have $title in each php file which refers to the title in the db.
Currently I'm using this in my current .htaccess file:
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]
And the following as href: <a href=\"cars/$id\"/>$title</a>.
So what I wish to have as my URL is the following. Basically I need to add the title to the current URL and replace the spaces between words in title with dashes.
http://www.domain.com/cars/543/new-porsche-is-out
cars -> subfolder
543 -> $id (From the database)
new-porsche-is-out -> $title (From the database)
You can just change your last rule to allow title in the end:
# Internally forward cars/231/title to cars.php?cars_item=231
RewriteRule ^cars/([0-9]+) /cars/cars.php?cars_item=$1 [NC,L,QSA]
Related
I am very frustrated looking for working clean URL using .htaccess:
My .htaccess code:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
RewriteRule ^/([0-9]+)$ product.php?id=$1
The first rule is percfectly working in hiding the .php extension of my URL but the second rule is not working. The URL of my product after clicking the product ID no on the product list. I'm using free host for now. freehost.16mb.com/product?id=156
My Code of my product list link is:
link 1 array
So I'll try to clean the URL using .htaccess into freehost.16mb.com/product/156.
I am using the .htaccess code above but it doesn't work for me. I need some help.
Your second rule does not make the provision for the leading /product before the ID, and your HTML anchor is using the format that you are rewriting to - this needs to be swapped around.
Change your anchor tag to the following:
<a href="/<?php echo $row['ID']; ?>
You also need to fix your rule up a bit by adding the QSA and L flags, and by removing the leading slash from the rule's pattern:
RewriteRule ^([0-9]+)$ product.php?id=$1 [QSA,L]
Once you've done that, /1234 will rewrite to /product.php?id=1234.
However, if you wish for the /product segment to be included, then use these instead:
<a href="/product/<?php echo $row['ID']; ?>
RewriteRule ^product/([0-9]+)$ product.php?id=$1 [QSA,L]
Just Use this in your .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/id/(.*)/ product?id=$1
RewriteRule product/id/(.*) product?id=$1
This will turn your URL into:
freehost.16mb.com/product/id/156
To have freehost.16mb.com/product/156 just remove the id/ from the RewriteRule, so it will be:
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/(.*)/ product?id=$1
RewriteRule product/(.*) product?id=$1
---- ALTERNATIVE ----
RewriteCond %{QUERY_STRING} (^|&)id=156($|&)
RewriteRule ^freehost\.16mb\.com/product$ /freehost.16mb.com/product/156?&%{QUERY_STRING}
I have a page in wordpress I am using with a slug called Bad-Debt-Recovery-in/. I am using a custom php query on that page with strings in the URL's like this
Bad-Debt-Recovery-in/?zipcode=55555&location=Chambers%20County+AL
How can I make this url into a slug like this
Bad-Debt-Recovery-in/55555/Chambers-County/AL/
as a rewrite? Any help would be appreciated!
UPDATE:
This code is actually what I am using. I also made it simpler and created a third variable named "state". One rewrite is for City and one is for County page:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agencies-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agencies-Services-In/?zipcode=$1&city=$2&state=$3 [QSA,L,NC]
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agency-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agency-Services-In/?countyid=$1&county=$2&state=$3 [QSA,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
# END WordPress
What you are asking for, and seeking to accomplish is called: Converting a request path into a query string.
Use .htaccess RewriteRule directive to modify the incoming request
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?a=$1&b=$2&c=$3 [QSA,L,NC]
Each ([^\/]+) captures path elements into a variables $1,$2,$3...
The ? at the end simply denotes that the last / is optional or else the last match could fail
the \ simply escape the '/' for literal interpretation
Add as many ([^\/]+) as needed and capture them in the query
zipcode=$1&location=$2+$3
The modifiers at the end [QSA,L,NC] are called flags
QSA appends the query string to the end of the rewrite if any exist
L simply says this is the last rewrite
NC means not case sensitive
This is your final solution:
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?zipcode=$1&location=$2+$3 [QSA,L,NC]
You can use the WP function sanitize_title
Combine this with a php loop i.e.
$url = $_SERVER['REQUEST_URI'];
foreach($_GET as $g){
$url .= '/'.$g;
}
$url = sanitize_title($url);
Sometimes I find that URLs on my website show up with mywebsite_smf_smf1_session=.
For example:
http://mywebsite.com/index.php?mywebsite_smf_smf1_session=6goevo28vf18ubqgb5uh6r08b2&topic=332242.msg916981
http://mywebsite.com/index.php?mywebsite_smf_smf1_session=6gqvo88lu5fnl1qmj7temt0113&topic=316196.msg931242
The part that contains mywebsite_smf_smf1_session=### shouldn't be in the URL, but the link still redirects to the correct page.
I would like to know how I can use mod_rewrite in .htaccess so that when someone clicks a URL containing mywebsite_smf_smf1_session=###&, it automatically reverts to the version not containing it:
Clicking:
http://
mywebsite.com/index.php?mywebsite_smf_smf1_session=6gqvo88lu5fnl1qmj7temt0113&topic=316196.msg931242
Becomes:
http:// mywebsite.com/index.php?topic=316196.msg931242
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mywebsite_smf_smf1_session=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^index\.php$ %{REQUEST_URI}?%1 [NE,L,NC,R=302]
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'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.