Forum Moderators: phranque

Message Too Old, No Replies

need a rewrite rule that only affects domain name

not the rest of the site.

         

nippi

4:26 pm on Jul 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i have a split a bunch of pages from my main site, into a subdomain.

All pages in the subdomain, have the same url they used to have in teh main domain.

Problem is.... there is no "home page" and when google asks for it, i want to redirect to the main url.

eg

[resources.mydomain.com...] = no redirect
[resources.mydomain.com...] = redirect to [mydomain.com...]

i can redirect index.php and index.html fine, but the url root itself... when i set a rule for it then EVERYTHING gets redirected


any ideas?

g1smd

4:28 pm on Jul 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use example.com to stop forum auto-linking.

Let's see your code. There's more than 10 000 threads with examples of redirects in this forum to get you started.

Use RewriteRule for this job. Do not use Redirect or RedirectMatch.

Further hint: questions like yours are asked several times per week, stretching back for a whole decade. :)

nippi

12:17 am on Jul 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well that's embarrasing, as i know mine is a dogs breakfast, and the line in question is not even close...You can see what i am traying to do. redirect the root, to an inner page. does not work, causes a loop.

oh well.

<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/index.php
RewriteRule ^(.*)$ [resources.mydomain.net.au...] [R=permanent,L]

RewriteCond %{REQUEST_URI} ^/index.htm
RewriteRule ^(.*)$ [resources.mydomain.net.au...] [R=permanent,L]

RewriteCond %{REQUEST_URI} ^/ (this is the problem line)
RewriteRule ^(.*)$ [resources.mydomain.net.au...] [R=permanent,L]

RewriteRule ^rss.xml$ rss.php?type=flex [L]
RewriteRule ^product-rss.xml$ rss.php?type=product [L]
RewriteRule ^blog-rss.xml$ rss.php?type=blog [L]
RewriteRule ^all-rss.xml$ rss.php?type=all [L]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^blog/archive-([0-9-]+) blog.php?m=$1&id=7 [L,QSA]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^blog/([0-9]+) blog.php?page=$1&id=7 [L,QSA]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^blog/ blog.php?id=7 [L,QSA]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^tag/([a-z0-9-]+) tag.php?t=$1 [L,QSA]

#RewriteRule ^flex/blog/7/1 blog.php?id=7&page=1 [R=301]
#RewriteRule ^flex/blog/7/1 /blog/ [R=301, L]
#RewriteRule ^flex\/blog\/7\/1 /blog/ [R=301, L]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule (.*)/(.*)/(.*)/(.*) $1.php?id=$3&page=$4&name=$2 [L,QSA]

# New cfm with page number ruleset

RewriteCond %{REQUEST_URI} -[0-9]+\.cfm$
RewriteCond %{REQUEST_URI} !//
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)-([0-9]+).cfm$ /$1.php?cat=$2&id=$3&rqvar1=$4&rqvar2=$5&rqvar3=$6&name=$7&page=$8 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)-([0-9]+).cfm$ /$1.php?cat=$2&id=$3&rqvar1=$4&rqvar2=$5&name=$6&page=$7 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)-([0-9]+).cfm$ /$1.php?cat=$2&id=$3&rqvar1=$4&name=$5&page=$6 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)-([0-9]+).cfm$ /$1.php?cat=$2&id=$3&name=$4&page=$5 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)-([0-9]+).cfm$ /$1.php?cat=$2&name=$3&page=$4 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)-([0-9]+).cfm$ /$1.php?name=$2&page=$3 [L,QSA]

# New cfm without page number ruleset

RewriteCond %{REQUEST_URI} \.cfm$
RewriteCond %{REQUEST_URI} !//
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+).cfm$ /$1.php?cat=$2&id=$3&rqvar1=$4&rqvar2=$5&rqvar3=$6&name=$7&page=1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+).cfm$ /$1.php?cat=$2&id=$3&rqvar1=$4&rqvar2=$5&name=$6&page=1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+).cfm$ /$1.php?cat=$2&id=$3&rqvar1=$4&name=$5&page=1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+).cfm$ /$1.php?cat=$2&id=$3&name=$4&page=1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+).cfm$ /$1.php?cat=$2&name=$3&page=1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+).cfm$ /$1.php?name=$2&page=1 [L,QSA]

#RewriteCond %{HTTP_HOST} /admin/index.php
#RewriteRule ^(.*)$ /admin/tracker/$1 [R=permanent,L]

# 1 week
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age= 604800, public"
</FilesMatch>

RewriteCond %{HTTP_HOST} ^www\.resources\.mydomain\.net\.au$
RewriteRule ^/?$ "http\:\/\/resources\.mydomain\.net\.au\/" [R=301,L]

g1smd

12:25 am on Jul 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use example.com to stop forum auto-linking.

Your rules are in the wrong order. You must list all external redirects before you start listing internal rewrites - otherwise the redirects expose internal server paths back out on to the web as new URLs. The www to non-www rule must be the last redirect before you start on the rewrites.

The redirects must include the domain name in the target. Some of your redirects are missing the domain name.

The index redirects must test THE_REQUEST so that only direct client requests for index are redirected. You don't want to redirect after DirectoryIndex has set the internal pointer to actually fetch the content from the index file.

Don't escape slashes in the literal target URL.

DO escape all literal periods in every RegEx pattern.

We'll revisit the rule with multiple (.*) patterns later, suffice to say that you should only use (.*) when it is the LAST item in the pattern, and you can never use more than one in a pattern. The (.*) means "everything, anything, or nothing". You're trying to say everything, followed by everything, followed by everything. If you put "everything" into $1 there's nothing left for $2 onwards.

nippi

5:55 am on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



my webserver addded the last rule there - yes i can move it...yes, there are some other issues and thanks for you help i will fix those issues...... but i could not see where you addressed my main issue which is to only redirect the home page of

resources.mydomain.com to mydomain.com

g1smd

6:46 am on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The path pattern you need is
^$
or
!.
as leading slashes are stripped before presentation to mod_rewrite.

However, you have a lot more important things to fix than a single URL returning "404".

nippi

11:19 am on Jul 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sorry.. i did not write this, my understanding of it is not too flash. my main web guy is away for 6 weeks so i am just trying to patch a hole till he returns.

"The path pattern you need is ^$ or !. as leading slashes are stripped before presentation to mod_rewrite. "

Can you give me the whole line please? I have no idea where to put this.

lucy24

9:35 pm on Jul 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"The path pattern you need is ^$ or !. as leading slashes are stripped before presentation to mod_rewrite."

Both of these are RegEx-speak for "nothing at all". The first version means "there is nothing between the beginning (that's what ^ means) and ending (that's what $ means)". The second version means "is not equal to, or does not contain (that's what ! means in Apache only) any random character (that's what . means)".

If you say
RewriteRule (.+) /newdirectory/$1

it means "take http://www.example.com/anyoldblahblah and send it over to http://www.example.com/newdirectory/anyoldblahblah"

because the first part of the rule always omits the http://www.example.com/ part. So if you want to pull out only requests for the index file, you say

RewriteRule ^$ http://www.example.com/ [R=301,L]
OR
RewriteRule !. http://www.example.com/ [R=301,L]

They are exactly the same, so you can use whichever form makes you feel less uneasy.

The index redirects must test THE_REQUEST so that only direct client requests for index are redirected.

%{THE_REQUEST} means "what the human user originally asked for". So the rule has to be preceded by

RewriteCond %{THE_REQUEST} [A-Z]+\ /index\.[a-z]+\ HTTP/1\.[01]

(plus a couple more variables that I forgot). Note that the "Request" contains two spaces-- before and after the filename-- which must be escaped \ so Apache knows that they are actual space characters. This is specific to Apache, not to Regular Expressions in general.

Depending on what your server does and when it does it, the middle part of the Request may have to be expressed as (/?index\.[a-z]+)? to include type-ins and bookmarks, where the user's browser might have said "domainname" alone, or "domainname/", or the complete "domainname/index.html".