Writing a RewriteRule - apache

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/

Related

How to create a redirect rule for 404 pages with many different path

Here my problem, the site I'm working on has many 404 pages, but they used to be the same pages with a different path
/mens/designers/mens/wales-bonner
/mens/designers/casual-shirts/wales-bonner
/mens/designers/coats-and-jackets/wales-bonner
etc.
THe client wants the redirect to go to the category, so
/mens/designers/mens/
/mens/designers/casual-shirts/
/mens/designers/coats-and-jackets/
I'm pretty sure, there must be a way to have regex rule to cover them all, but I can't seem to find how
Something like
RewriteRule ^/mens/designers/(.*)/wales-bonner /mens/designers/(.*)
but it doesn't work, I don't know how to group the middle part of the URL
Can anyone help ?
I see several potential problems with your rewrite rule:
You have a capturing group in the output URL rather than using $1 to use the result of the capturing group from the input URL.
^/ won't work in .htaccess, only in Apache .conf files. I' would use ^/? instead which will work in either context. That makes the starting slash optional.
You don't include an [R] flag on the rule to make it a redirect.
You don't include an [L] flag on the rule to prevent any following rules from interfering with it.
You can also add "mens/designers" to the capturing group so that you don't have to repeat it in the output. I would suggest:
RewriteRule ^/?(mens/designers/.*)/wales-bonner /$1 [R=301,L]

multiple folder redirect

I have been trying variations of the following without success:
Redirect permanent /([0-9]+)/([0-9]+)/(.?).html http://example.com/($3)
It seems to have no effect. I have also tried rewrite with similar lack of results.
I want all links similar to: http://example.com/2002/10/some-long-title.html
to redirect the browser and spiders to: http://example.com/some-long-title
When I save this to my server, and visit a link with the nested folders, it just returns a 404 with the original URL unchanged in the address bar. What I want is the new location in the address bar (and the content of course).
I guess this is more or less what you are looking for:
RewriteEngine On
ReriteRule ^/([0-9]+)/([0-9]+)/(.?)\.html$ http://example.com/$3 [L,R=301]
This can be used inside the central apache configuration. If you have to use .htaccess files because you don't have access to the apache configuration then the syntax is slightly different.
Using mod_alias, you want the RedirectMatch, not the regular Redirect directive:
RedirectMatch permanent ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3
Your last grouping needs to be (.+) which means anything that's 1 character or more, what you had before, (.?) matches anything that is either 0 or 1 character. Also, the last backreference doesn't need the parentheses.
Using mod_rewrite, it looks similar:
RewriteEngine On
RewriteRule ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3 [L,R=301]

ZendFrameworks not working nice with RewriteRule

I have the following RewriteRule on my Apache with ZendFramework in the back:
RewriteRule ^/account([0-9]+) /account?i=$1 [L]
Problem that I'm having is that I have an AccountController.php for ZendFramework, so URL from browser going to ...://myserver/account works just fine. However, when I have this rewriterule in httpd.conf, and point my browser to ...://myserver/account1, then ZendFrameworks complains that /account1 controller is not found, even though I have the rewriterule in apache. My guess is that ZF is looking at the REQUEST_URI which still have /account1 in it.
Anyone knows now I can make this rewriterule work properly with ZF? What I need is basically to have URLs going to /account([0-9]+) forward to /account?i=$1 instead using my ZF's AccountController.php?
Note I do not want to 'expose' the 'i' param to users (for various reasons, and yes, they could guess at looking at account1 that it can be sequenced), hence it is an internal forward instead of an external redirect back to the browser.
Thanks!
Maybe your rule is overwritten by other Zend rules? By default, Zend takes URLs in the form controller/key1/value1/key2/value2/..., maybe you could just try this?

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.

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.