Total noob with redirect and rewrite, redirect everything exept for one map

Topics: Developer Forum, Project Management Forum, User Forum
Apr 1, 2011 at 1:12 PM
Edited Apr 1, 2011 at 1:13 PM

Hi, i just started with building websites after being out of it for like 9 years, and i rebuilded a website on a new domainname, but there is one map on the old domain which i would like to be accessable.

What i want to do is redirect every request for to except the requests for*.*, they should function as usual.
Thanks for the help already.

Apr 1, 2011 at 10:02 PM
Edited Apr 1, 2011 at 11:09 PM

Since the websites page all had .htm extention, and the files in the map i still want to use are .php scripts, i made the rules like this:


RewriteCond  %{HTTP_HOST}            ^(www\.)?mydomainnamehere\.nl$
RedirectRule ^/(.+)\.(htm)$      [R=301]

Only noob issue i have now when people request, it wont be redirected, im managing that issue with a basic html redirect: 

<meta http-equiv="REFRESH" content="0;url=">
Apr 2, 2011 at 2:40 PM

So you're all good, then?  Everything is working?

Apr 2, 2011 at 11:36 PM

Well, no, i would like to redirect everything exept requests for*.*, i know im pretty close with how i wrote the rule i use now.

i would like to know an expert opinion about what would be best to do in this case.

Im getting pretty loaded with information about all kinda scripting languages and all after being out of this kinda stuff for quite some time, pretty confusing for me sometimes :)

Apr 3, 2011 at 9:42 PM
Edited Apr 3, 2011 at 9:47 PM
RewriteCond %{HTTP_HOST} ^(www\.)?mydomainnamehere\.nl$
RedirectRule ^/(.+)\.(htm)$ [R=301]

Im so noob lol, this works like a charm already, since the scripts dont use .htm as extention, every php script is accesable, and what should be redirected is already being redirected.
I should probably quit for some sleep earlyer :D

The only thing what i would like is when the request ends with a slash, it also gets redirected. What should i change for it?
Apr 4, 2011 at 5:36 PM

I think that rule will redirect all requests made to the old domain name, to the root document of the new domain name.  Is that what you want?
Sometimes people try to keep the page names intact across a redirection. In other words, you could redirect a request for old/intro.htm to new/intro.htm . Your rule does not do this. To modify the rule so that it does keep pages intact, you'd need to do something like this:

RewriteCond %{HTTP_HOST} ^(www\.)?mydomainnamehere\.nl$
RedirectRule ^/(.+)\.(htm)$$1.$2 [R=301]

 As for also redirecting if the request ends with a slash, you could just add an additionalrule:

RewriteCond %{HTTP_HOST} ^(www\.)?mydomainnamehere\.nl$
RedirectRule ^/(.+)/$$1/ [R=301]

I also suggest that you include comments in your ini file describing what you intend the rule to do. That's just good coding practice, and as I always say, the rules for IIRF are code.

Apr 4, 2011 at 6:48 PM
Edited Apr 4, 2011 at 10:38 PM

I dont want to keep the original name intact, lemme explain a bit. The old website was a normal html based website, and i changed to a cms, so the files are also changed now. All the information is now in articles on the main page, so everything can get a redirect there. However, my scripting skills arent that good yet, there is a reservation script, which points to the mysql database on the old domain. The old domain is gonna be ours anyway, so i want the forms to be loaded from the old domain, so i can test the new forms i am gonna use in the futures on the new domain, and leave the old reservation intact, and still use it , just by loading it from the old server through the cms.

Anyway, im thinking im totally wrong with my code anyway, i dont need a rewritecond i guess.
I just want to redirect some stuff from the old domain to the new domain, but i still want something from the old domain being accessable.

RedirectRule ^/(.+)\.(htm)$

But, now when people request my website like this:
It doesnt get redirected

But if the request is like this: or
It does get redirected.

The solution for me is really simple, but i just didnt see it, because i didnt think it would be this simple. Im feeling so stupid i didnt just see it right away.. :D

RedirectRule ^/$
RedirectRule ^/(.+)\.(htm)$


Apr 5, 2011 at 4:39 PM

Yes, what you wrote last in your post, should work.

The reason the first rule, alone, did not work for the simple case, is that you have a \. in the regex pattern, which matches a dot.  You could replace both rules with

RedirectRule ^/.*$     http://newdomain.dl/

This will match any incoming URL with zero-or-more characters in the URL.

Apr 5, 2011 at 5:18 PM
Cheeso wrote:

The reason the first rule, alone, did not work for the simple case, is that you have a \. in the regex pattern, which matches a dot. 

Yeah i figured that out.

But, my mistake was to think i could write it into just one redirectrule, maybe it is possible, but im not that advanced yet.

I dont think i can use the rule you wrote, requests for should be accesable

Apr 6, 2011 at 11:46 AM

I now still have some issues with google search results linking to this: http://<cite></cite>

How can i get that redirected to my new domain, or rewritten to without tempering with other php files? i guess it should use the front.php? as a condition, but how to formulate this rule?

Thanks for the help so far

Apr 6, 2011 at 1:35 PM
Edited Apr 6, 2011 at 1:40 PM

Maybe this makes a better example of what i would like to see. Its not working how it is written below, but it shows my needs.



# do not rewrite requests for php scripts in the map /aanmelden
RewriteRule ^/aanmelden/(php)/(.*)$ - [L,I]

# this is the rule that does the work. It redirects everything
# else to
RedirectRule ^/(.*)$ [R=301]
Apr 7, 2011 at 2:40 PM
Edited Apr 7, 2011 at 2:42 PM

Ok, regarding this:

# do not rewrite requests for php scripts in the map /aanmelden
RewriteRule ^/aanmelden/(php)/(.*)$ -  [L,I]

..that probably won't do what you want.

I think you want to NOT REWRITE php scripts in the folder "aanmelden". That means, the incoming URL is like /aanmelden/foo.php , is that right? If so, then the regex that matches the url path is ^/aanmelden/[^\.]+\.php . Therefore to exclude those incoming URLs from rewrite, you should use

# do not rewrite requests for php scripts in the folder /aanmelden
RewriteRule ^/aanmelden/[^\.]+\.php  -  [L,I]

The hieroglyphics translate this way:

  • ^ is a beginning-of-line assertion. This regex matches only at the beginning of a "line", or in this case, at the beginning of the URL path. 
  • /aanmelden matches itself. This means the URL path must start with "/aanmelden" .
  • square brackets denote a range. Square braces with a circumflex (^) as the first character within the square brace denotes a negated range. The \. matches a dot.  (dot has a special meaning in regex: it is a wildcard matching any character. To match a dot, we need to "escape" the dot by preceding it with a backslash.)  Therefore [^\.] is "any character that is not a dot".
  • The + is a quantifier, implying "1 or more". It applies to the preceding atom in the regex, which is the negated range. Therefore [^\.]+ says "a set of 1 or more characters none of which is a dot."
  • \. matches a dot
  • php matches itself.
  • we don't use a $ terminator here, because the incoming URL may or may not have a query string. Either way, we don't care - the desired action is the same: don't rewrite. The important thing is that the regex starts with a php script.

I hope that helps clarify some things.

good luck.

Apr 7, 2011 at 3:03 PM

Okay, so if i would put in a $ terminator it would exclude .php files with a string attached to it, like this:

# do not rewrite requests for php scripts in the folder /aanmelden
RewriteRule ^/aanmelden/[^\.]+\.php$  -  [L,I]

would include files like

But, would exclude requests like this if the string wouldnt contain .php
/aanmelden/register.php?=registerform.php (probably not if its written like this because of the .php in the string, but it is one of my issues with results in google which need to be rewritten)

Apr 7, 2011 at 3:43 PM
Edited Apr 8, 2011 at 2:11 PM

yes, correct.

Adding the terminator makes the regex match a URL that ends in .php.  "The string attached to it" as you call it, is also known as "the query string".  It is the optional part of a URL that follows the ? .  So for example, in the URL http://server/register.php?id=192 , http is the scheme, server is the server name, /register.php is the url path, and id=192 is the query string. These are well-known names for elements of a URL.

Getting back to the regular expression matching, a pattern like "^/[^\.]+\.php$" (with a terminator)  matches "/register.php" but not "/register.php?id=192" .  The latter does not end in .php , which is why it does not match.

Remember that the piece in the regex pattern that precedes .php is [^\.]+, which as I said before, means "1 or more characters, none of which is a dot."   Therefore the pattern "^/[^\.]+\.php$"  also will NOT match "/register.php?regform.php" because, while that hypothetical URL does end in .php, the text preceding the final .php, (register.php?regform) includes a dot. 

I don't know what your google issue is; if you explain more in terms of what you need to rewrite engine to do, maybe I can help.

 edited for correctness and clarity.

Apr 7, 2011 at 3:54 PM

I finally got my eureka moment, thanks for the last explanation. Finally no results in google anymore which link to a 404 not found page on my domain :)

Apr 8, 2011 at 2:12 PM

Great, glad to hear it, and good luck.