Removing $id from url with .htaccess - php

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.

Related

Remove %2523 from url and redirect to new, without that using .htaccess

I don't have an idea how can I remove %2523 from URL made by GET in php. I want to redirect users to page without that. Sometimes it has # on string beggining, and that's why it generates that "%2523".
For example, want to redirect them from something like:
localhost/catalog/value/%2523string
to
localhost/catalog/value/string
My current .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/catalog/value\.php\?value=([^/]*)\s [NC]
RewriteRule ^ /catalog/value/%1? [R=301,L]
RewriteRule ^catalog/([^/]*)$ /value/value.php?color=$1 [L]
Hopefully you can help me with that, trying from yesterday and still didn't nothing works.
Finally i solved that by adding this at the .htaccess beggining:
RewriteRule ^([^%]*)\%23(.*)$ /catalog/value/$2 [R=301,L]

mod_rewrite query strings returning 404

I have a custom URL like this:
website.com/show/?id=9999&n=page-name
I'm trying to come up with a rewrite rule to convert it to:
website.com/show/9999/page-name/
Note that this is a WordPress site and /show/ is WP page name.
Here's the rules I'm using in .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^show/(.*)$ /show/?id=$1 [R=301,NC,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is working; it rewrites website.com/show/?id=9999 to website.com/show/9999/.
Then I modified the rule for the second query string:
RewriteRule ^show/(.*)/(.*)$ /show/?id=$1&n=$2 [R=301,NC,QSA]
But now website.com/show/9999/page-name/ returns a 404 error. It works if I go to: website.com/show/9999/?n=page-name.
What am I doing wrong?
Update
The 404 problem is now solved.
However, now I need to redirect the old query string URL:
website.com/show/?id=9999&n=page-name
to the new SEO friendly URL:
website.com/show/9999/page-name
How do I setup that redirect?
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^show/(\d+)/?([^/]*)/?$ /show/?id=$1&n=$2 [L,NC,QSA]
# Wordpress defaults:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
## Results
# show/9999 => show/?id=9999&n=
# show/9999/ => show/?id=9999&n=
# show/9999/page-name => show/?id=9999&n=page-name
# show/9999/page-name/ => show/?id=9999&n=page-name
As you see, when page-name is not available, there will be an empty n query string parameter. In your PHP script, instead of checking for the parameter presence using isset(), try using empty().
One last note; The above rules do not redirect the request, it maps the request under the hood, so that the URL stays clean. If you need a redirect, just add a R flag.
404
Even if you remove your custom rewrite rules, you'll get a 404 from WordPress. That's because, as you said, the rewrite target (/show) is a WordPress page and ultimately get mapped to the index.php file. Then, WordPress checks its database to see if it can find a page with that path or throws a 404 if it can't. Obviously, the latter is your case.
Update
Regarding your recent update; To redirect the old URL to the new one, you need some rewrite rules along the lines of:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=(\d+)(&n=)?(.*)$
RewriteRule ^show/?$ /show/%1/%3 [R=301,QSD,L]
# Wordpress default rules:
# ...
</IfModule>
## Results
# show?id=9999 => show/9999
# show?id=9999&n=page-name => show/9999/page-name
# show/?id=9999&n=page-name => show/9999/page-name
Don't use [R=301] until you're 100% sure that everything's working fine, as it's aggressively get cached by the browser.
I think I figured it out... almost.
#sepehr is correct that WordPress is checking, not finding the page, and throwing a 404. So I needed to use WordPress' own rewrite engine to define the rewrite rules so it recognizes what I'm doing.
So I added this to my WordPress theme's functions.php file:
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^show/([^/]*)/([^/]*)/?',
'index.php?page_id=2382&id=$matches[1]&n=$matches[2]',
'top'
);
}
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'id';
$vars[] = 'n';
return $vars;
}
Now the URL website.com/show/9999/page-name works correctly, not throwing a 404.
However, now I need to redirect the old query string URL to this new one. See my updated question.
Update
Here's the rewrite rule to redirect the old query string URLs to the new SEO friendly URLs:
RewriteCond %{QUERY_STRING} ^id=([^/]*)&n=([^/]*)$
RewriteRule ^show/?$ /show\/%1\/%2\/? [R=301,L]
Please check below rule in to your .htaccess.
RewriteRule ^show/(.+)/(.+)$ show/?id=$1&n=$2 [L,QSA]

Add $title to url with .htaccess file

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]

.htaccess with multiple rules

I want to create an htaccess file with the following rules:
I want to redirect:
http://computingessentials.tk/episodes/6
To:
http://computingessentials.tk/episodes.php?id=6
Same applies with the remaining episode.php ids
Next, I want to redirect:
http://computingessentials.tk/episode.php?id=56
To:
http://computingessentials.tk/episode/56
Same applies with the remaining episode.php ids
And last but not the least, I want to remove all the .php extensions of the files.
Since I am farlynew to htaccess i don't really know how to get all of these in an htaccess.
Help would be very much appreciated.
Make sure your mod rewrite of apache is on and put these rules in .htaccess
RewriteEngine on
RewriteRule ^episodes/([0-9]+)$ episodes.php?id=$1 [L]
You can use these 2 rules in your root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episodes\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /episodes/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^episodes/([^/.]+)/?$ /episodes.php?id=$1 [L,QSA,NC]

How to use htaccess code for URL rewrite in PHP?

I have a link like below:
In /country/search.php
<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">
When I use the below .htaccess code, it does nothing:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L]
It does in the tool:
http://example.com/0/0/4/122
... but when I click that link it shows old URL again.
What is the problem here?
I'm generating that .htaccess code using this tool: http://www.generateit.net/mod-rewrite/
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]
# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]
I think you misunderstood what the rewrite rules are trying to achieve in this context.
F.e. this rewrite rule
RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2
Will ensure that a request to http://example.com/mymethod/myarg will be rewritten to
http://example.com/app.php?method=mymethod&arg=myarg
When you generate links from within your application you should know how you contstructed the rewrite rules and you have to build the link accordingly.
Original URL:
href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>
Ideal URL:
/India/andhra%20Pradesh
And .htaccess code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

Categories