Make a htaccess rule change the url - apache

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]

Related

How to set the right conditions in htaccess file?

I've tried for ages to fix this problem that I have.
On my website I have rewriten some links with the htaccess file. The htaccess file currently looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?sideID=$1
RewriteRule ^([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)$ index.php?sideID=$1&id=$2&title=$3
RewriteCond %{HTTP_HOST} ^www\.mypage\.no$
RewriteRule ^/?$ "http\:\/\/mypage\.no\/" [R=301,L]
The first rule rewrites from:
http://mypage.com/index.php?sideID=home
To this:
http://mypage.com/home
The first rule rewrites from:
http://mypage.com/index.php?sideID=b&id=12&title=a-great-blog-post
To this:
http://mypage.com/b/12/a-great-blog-post
Soo they do what I want too when it comes to rewriting I guess. BUT, the problem is that if im now navigated to any blog post on the site and then tryies to navigate back to whatever other link lets say http://mypage.com/home it will add http://mypage.com/b/12/home instead. That can of course be fixed easily by just using absolute URL's in the navigation. But when I try to make a XML Sitemap, the bot will index every page for each blog ID like so: http://mypage.com/b/12/home, http://mypage.com/b/13/home, http://mypage.com/b/14/home and so on for each blog ID and pagename.
Is there a way to make sure the RewriteRule's apply seperatly or set a condition for them or something like that?? I am a bit noob in this area.
I beg you, please help me! :)

URL rewriting and redirection

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 :)

Using .htaccess mod_rewrite to pass all URLs in a given directory to a single redirect script

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]

URL rewriting that visibly rewrites (changes the URL in the address bar)

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/

Rewrite To WordPress Page

I have an existing page of /programs/kids.php that I want to load a category page from WP. I want the .htaccess file in /programs to handle this rewriting for me.
Something along the lines of:
RewriteEngine on
ReWrite kids2.php http://www.mysite.com/blog/cat/kids/
Any help would be awesome.
If your sure mod_rewrite is enabled by apache, the it is simple as
RewriteEngine on
RewriteRule kids.php http://www.mysite.com/blog/cat/kids/
or
RewriteEngine on
RewriteRule ^programs/kids.php$ http://www.mysite.com/blog/cat/kids/
depending on location and strictness (^hhh$ matches the whole string)
The directive is named RewriteRule and not just Rewrite. So try this:
RewriteRule ^programs/kids2\.php$ /blog/cat/kids/
But if you want have requests to /blog/cat/kids/ rewritten internally to /programs/kids2.php (the exact opposite of what you’ve mentioned), try this rule:
RewriteRule ^blog/cat/kids/$ programs/kids.php [L]
You could also do an include for that kids.php in the content of an existing WP page, but you'd need the ExecPHP plugin installed.
Then kids.php would display as styled content inside a WordPress page. Which is sometimes desirable, from a site cohesiveness standpoint.
Something like this:
<?php require_once(ABSPATH. '/programs/kids.php');?>
Not exactly what you asked for, but maybe an alternative approach.