Rewrite To WordPress Page - apache

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.

Related

Writing a RewriteRule

I have a website that recently had a domain name change, and I am trying to set the rules up to include ANYTHING that is after the ruleset to be redirected (sub domains currently work, but file exensions do not) The current ruleset I have in place is:
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com [R=301,L]
If I go to www.example.com/subdomain/ or www.example.com/subdomain/another_directory/ it redirects to the appropriate site, but if I go to www.example.com/subdomain/file.ext or www.example.com/subdomain/another_directory/file.ext, the redirect does not work. I know I need to change the regex so that it works correctly, but i'm not sure what it needs to be changed to.
You are not using captured group in target URL. Use it like this:
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com/$1 [R=301,L,NE,NC]
You don't need mod_rewrite for this.
A simpler, lighter approach would be
RedirectMatch ^/subdomain/ http://subdomain.example.com/

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

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]

Using mod_rewrite to put words in the URL instead of category IDs

I'm working on a Business Directory Website. What I'd like to do is transform the URL String of :
http://www.website.co.uk/business/results.php?category_id=11
To http://www.website.co.uk/business/Financial & Legal
How do I achieve this? Not too sure on the Rewrite Rules etc I am to use.
Thanks for any help, in advance.
I am now trying jut a test on a local host.
The HTML Code is as follows :
<h1>This is the PHP file.</h1>
Link Here
So all I want is the URL to be http://localhost/mod_rewrite/index.php/category
rather than : http://localhost/mod_rewrite/index.php?url=category
The .htaccess file is as follows (But doesnt appear to work)??
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?url=$1 [L]
Well,
in general you don't use ampersands in URLs.
They will be replaced by "%26" when you try to call them as a link.
But a general approach towards your question could look like that:
RewriteEngine on
RewriteRule ^business/Financial.*?Legal$ results.php?category_id=11 [L]

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.