I just finished my rewriting with .htaccess and everything works just fine.
RewriteEngine on
RewriteRule blog/([a-zA-Z0-9\-]+)-([0-9]+) post.php?url=1&id=$2
The only I want to avoid is the duplicate content... I've searched a lot on the subject and sadly found anything matches my needs.
The fact is, the rewrited address "blog/my-new-post-77" is also accessible by "post.php?id=77" and I don't want it to happen. So I would like to redirect every post.php pages to the rewrited rule.
Someone have an idea for me?
Yes, add extra variable to your rewrite rule to check it:
RewriteRule blog/([a-zA-Z0-9\-]+)-([0-9]+) post.php?check=ok&url=$1&id=$2
And at the top of your post.php file, check for that check variable:
if(isset($_GET['check'])){
if($_GET['check'] == "ok"){
//it comes using rewrite rule
//cut and paste all of your codes in post.php file to here
//that is which codes are available when user view your page in desired way
//don't redirect to rewrite rule again here, since visitor came using that rewrite rule to here. doing so will result infinite redirect and will show an error
}else{
//this visit is not used rewrite rule to come here
//here you can redirect visitor to not found page or just show an empty page by putting die();
//you can't redirect to post pages here, because you don't know what post(id) that visitor need to see
}
}else{
//this visit is not used rewrite rule to come here
//here you can redirect visitor to not found page or just show an empty page by putting die();
//you can't redirect to post pages here, because you don't know what post(id) that visitor need to see
}
post.php?id=77 -> /my-new-post-77 via the browser
/my-new-post-77 -> post.php?id=77 via internal rewrite
Is it right?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule blog/([a-zA-Z0-9\-]+)\-([0-9]+)$ post.php?url=1&id=$2 [L]
RewriteCond %{THE_REQUEST} \s\/post\.php\?id\=(\d+)\s
RewriteRule . /my-new-post-%1 [R,L]
I found a solution which matches my needs waaaay more.
if($data["url"]!=$_GET["url"]) {
header("Location:http://www.mywebsite.com/blog/".$data["url"]."-".$data["id"]);
}
This solution forces the post.php?id=XX to go to the rewrited location as we wanted and by the same time, encounter any manual url rewriting.
$result being the SELECT ALL of my databases rows.
$data = mysqli_fetch_array($result);
I tested #Janaka solution, with your great explanations, and it worked tho.
Thanks everybody; Case closed :)
Related
Sorry if this has already been asked here, but I haven't found it through looking yet.
I've been asked to remove the /page on our site, and just make it go to the homepage instead. This is easy enough to do in PHP, but I'd rather use my .htaccess file if I can (to learn it better mostly).
In PHP I'd do (some psuedo code)
if($urlIsSlashPage){
header('Location: /');
die();
}
Is something like that possible to port to my .htaccess file?
I did have this,
RewriteRule ^page/$ / [NC,L]
And whilst that did show the home page, the URL in the address bar stayed as /page, I want that to display just / instead.
You can try with a redirect.
RewriteRule ^page/?$ http://www.domain.com/ [NC,L,R=301]
I have a few URLs that are now 404ing due to products expiring, and I need to redirect them to a related page that does exist.
e.g.
http://www.example.com/package-product.php?id=72 to http://www.example.com/best-sellers/
http://www.example.com/package-product.php?id=36 to http://www.example.com/bedroom-furniture/
etc.
As I understand it, I can't do this with a Redirect 301 because of the GET param, and the examples I've seen for rewrite seem to be a bit more generic and use a placeholder for the params - I need to send a specific URL to a specific alternative.
Thanks.
I'd probably do it the way #BobLiu suggested - i.e. to do it in PHP itself.
If that's not possible if you really want a rewrite solution, you can look at the QUERY_STRING variable available to mod_rewrite:
RewriteCond %{QUERY_STRING} id=72$
RewriteRule ^.*$ http://www.example.com/best-sellers/ [R=301,L]
Why not just say on the package-product.php page something like:
switch ($_GET['id'])
{
case 72:
[do redirect url1];
case 36:
[do redirect url2];
etc...etc..
}
For some reason google indexed several pages of my website as:
http://myapp.com/index.php/this-can-be-enything/1234
Now, I want to redirect with apache .htaccess those pages to correct urls:
http://myapp.com/this-can-be-enything/1234
I've googled and tried many options but with no success.
Any tip will be helpful.
I've added to my .htaccess file following lines:
RewriteCond %{THE_REQUEST} ^.*index.php.*
RewriteRule ^(.*)index.php(.*)$ $1$2 [NC,R=301,L]
I don't know if this is best solution but works ok for me.
Two Parts of problem
To make Google aware that indexed page is moved to some other destination you need to handle that # apache level and issue 301 ( moved permanently )
Handler to handle the cached requested URL to new URL using the #1 handler itself.
Im trying to use mod_rewrite to redirect any call to /real-estate/* to rewrite.php...i know i can redirect everything to rewrite.php with this:
RewriteRule ^(.*)$ rewrite.php?url=$1 [L]
I would like to have my urls formatted like /real-estate/12345/123-anywhere-st ....where the 123-anywhere-st would be ignored, and have /real-estate/12345 sent to rewrite.php...id like the rewrite rule to only be used on /real-estate...all other areas of the site should function as is...Ive searched all over for a good tutorial or cheat sheet, but none that I can find actually explain how to format the mod_rewrite rules, they just give one or two examples and thats it...can anyone help, as well as maybe provide a link to somewhere I can learn
Thanks!
RewriteRule ^/real-estate/(.*)$ rewrite.php?url=$1 [L]
I asked sort of the complement of this question before:
Mod_rewrite invisibly: works when target is a file, not when it's a directory
Now I actually want a rewrite to happen visibly, because I've switched URL schemes and although I want the old links to work, I want the user to see the new URL scheme.
So this works
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1
But the URL in the address bar remains as http://example.com/oldscheme/foo.
What's the right way to do a visible rewrite, preferably just with mod_rewrite as opposed to something kludgy with Location redirects or somesuch?
As I cannot leave comments now, I'll post my addition to Ignacio's comment here.
You actually should post a 301 (Moved Permanently) redirect, as you're describing there's a new site directory structure. So your RewriteRule should read
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1 [R=301]
It turns out adding a "redirect" code does the trick:
RewriteRule ^oldscheme/(.*)/?$ newscheme/$1 [R]
Obvious in retrospect, but hopefully this makes the answer more searchable.
I found it on this excellent "cheat sheet":
http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/