homepage Welcome to WebmasterWorld Guest from 54.196.196.62
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Htaccess Redirecting Questions
scaggster




msg:4537936
 11:43 am on Jan 21, 2013 (gmt 0)

Hi all been a fan of this site and used it for years from the outside without having to ask questions.

For years i have had sub domains on my sites but because google is so against duplication i am going to condense everything down to one supersite instead of say 20 odd subdomains,

i am looking for an htaccess that changes say subdomain.example.com/blah.html to a new url at example.com/blah.html but if possible the old subdomain index page which would be subdomain.example.com/psp to example.com/psp

Is it possible to redirect an index page to a folder but send the content pages direct to the new url?

Any help or advice would be great, because of the system i use i cant afford to have it redirected to example.com/psp/blah.html as that would still cause duplication problems.

 

g1smd




msg:4537941
 12:20 pm on Jan 21, 2013 (gmt 0)

htaccess cannot "make" nor "change" URLs.

To "change" or "make" a URL, you link to
href="/pagename" from the pages of your site, where "/pagename" is the URL that you want users to "see" in their browser status bar when they hover over the link and "see" in the browser address bar after the link has been clicked.

Once that link has been clicked, the htaccess rules are used to match the requested URL and the rule that matches simply modifies Apache's internal file pointer to point at where in the server filesystem the content really resides.

You can map any URL request to any internal location in the server filesystem. It's all in the way the rules are crafted.

It's important to realise that htaccess does not make URLs for content, it instead finds the content inside the server based on the URL that was requested, and that in turn relies solely on what you put in the "href" of the link that was clicked on.

There are some default "rewrite" actions aleady built in. You request "/" and the server tries to look in "/index.html" or "/index.php" to fulfil the request. Likewise for folders. You can add whatever extra rewrites you need.

If a site is already live and URLs are indexed, then once the rewrite is working, you also need more rules to redirect requests such that when an old URL is requested, the browser is told to make a new request for the new URL.

RewriteRule can be configured as an internal rewrite or as an external redirect. You'll need both to complete this job.

Parallel conversation: [webmasterworld.com...]

lucy24




msg:4538134
 2:37 am on Jan 22, 2013 (gmt 0)

i am looking for an htaccess that changes say subdomain.example.com/blah.html to a new url at example.com/blah.html but if possible the old subdomain index page which would be subdomain.example.com/psp to example.com/psp

Do all of your subdomains pass through the same htaccess? If so you have got it made because you will probably be able to delete about 90% of your current rules, leaving only the canonicalization rule. That's the one that goes (DO NOT cut & paste, I'm doing this off the top of my head)

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

The Condition will pick up anything that isn't your primary domain name in its canonical form-- including any and all subdomains. You have, or should have, some version of this rule in place already. All you've done now is erase any previous rules and/or conditions that pulled out the subdomains.

The immediately preceding rule already is-- or should be-- the one that grabs any request for explicit "index.html" (or whatever extension you use) and redirects to the directory alone.

I don't get the difference between "blah.html" and "psp". It seems to be the identical process.

All of this is assuming that every page on every subdomain has an exact match on the primary domain, so for every

subdomain.example.com/directory/foo.html
othersub.example.com/widgets.html
thirdsub.example.com/directory/subdirectory/

you already have a corresponding

www.example.com/directory/foo.html
www.example.com/widgets.html
www.example.com/directory/subdirectory/

If this is the case, I can see why you want to dump the subdomains ;)

scaggster




msg:4538931
 12:53 pm on Jan 24, 2013 (gmt 0)

Thanks for the replies seems i made a balls up above

Basically what i should have said was this

I need a way to redirect

subdomain.example.com to example.com/psp - Thats for the Index page only
for the rest of the subdomain

subdomain.example.com to example.com - Thats for all the content pages and Not the Index page.

Reason for this is so that i can use the new index page to point to the old news/content and not see it lost in google.

lucy24




msg:4539134
 2:26 am on Jan 25, 2013 (gmt 0)

It might help if I understood what "psp" means.* Do you want all your subdomains to redirect to the same file, or will there be a separate one for each subdomain?

Either way, the body of the rule will have the pattern

^(index\.html)?$

because you're grabbing two groups of requests: the ones who ask for "index.html" by name, and the ones who ask for the index page by its proper name, i.e. nothing. So the body of the rule will look at requests for "exactly index.html or exactly nothing".

Unlike your normal index-file redirect, this will have to come before the other named-file redirects, because "index.html" is a subgroup of "blahblah.html". And also unlike the normal index redirect, you will not need to look at THE_REQUEST, because there won't be any internal requests for this file. Instead the Conditions will look at the HTTP_HOST and pull out all the requests for subdomains-- either all of them collectively, or one at a time.

Do you have a whole lot of subdomains, or a fairly short finite list? In general, rules will run a lot more efficiently if you can tell them the exact text to look for:

(one|two|three|four)

rather than

\w+ or [a-z]+ or [^/.]+


* Memo to self: create macro for "It might help if I understood what X means".

scaggster




msg:4539484
 12:21 pm on Jan 26, 2013 (gmt 0)

Hi sorry, i was just using psp as an example, i think this is what i mean better.

a way to redirect

subdomain.example.com/index.php to example.com/folder/index.php - Thats for the Index page only
for the rest of the subdomain

subdomain.example.com to example.com - Thats for all the content pages and Not the Index page.


I can put an htaccess in each of the subdomains, so once i have one working, itll be easy to change what i need for each different subdomain, got 30 subdomains in total but the majority of content is made automatically from our forum, so bar the index page everything else can just be redirected to the new domain.

Thanks for your help, ive searched for hours on this subject but havent found anyone who seems to want what i am after, and i am totally clueless on htaccess :P

lucy24




msg:4539508
 2:57 pm on Jan 26, 2013 (gmt 0)

I can put an htaccess in each of the subdomains

There is no point to this, because you will be intercepting the requests before they get to the subdomain. Otherwise you'd have to make thirty separate htaccess files, each saying exactly the same thing: first a rule to redirect ^(index\.html)?$ to the new place, and then a second rule to redirect everything else to the main domain. And you would still need something complicated in the top-level htaccess to do the regular index-and-canonicalization redirects for everything that isn't a subdomain.

The part I didn't get is whether each subdomain's top-level index page gets a different /folder/ redirect, or whether they all go to the same place.

Thirty is an awkward number. It's in the grey area where you can either do it in htaccess or send everything to a php script, but it isn't big enough or small enough that you absolutely have to do one or the other.

Do the subdomains have subdirectories? That is, anything in the form
subdomain.example.com/directory/index.html
subdomain.example.com/directory/subdirectory/pagename.html

as opposed to simply
subdomain.example.com/pagename.html

If so, where will they go?

scaggster




msg:4539815
 11:35 am on Jan 28, 2013 (gmt 0)

i tried this
RewriteEngine on Options +FollowSymlinks -MultiViews
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteRule (.*)$ example.com/$1 [R=301,L]
RewriteRule ^(index)$ example.com/examplepage.php [R=301,L,NC]

this works if i want to redirect a content page ie subdomain.example.com/contentpage.php to example.com/contentpage.php it changes correctly but the index page doesnt go to the url im trying to send it to, which would be example.com/examplepage.php

This changes what i originally asked but i hope i make more sense now ( iknow what i mean myself but not easy to specify)

scaggster




msg:4539820
 12:04 pm on Jan 28, 2013 (gmt 0)

Looks like the simplest way to do it is redirect everything but not the index page, i can redirect that myself, time to research more

scaggster




msg:4539822
 12:24 pm on Jan 28, 2013 (gmt 0)

ive had to do it a funny way but this seems to work for me

heres the htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain.example\.co.uk [NC]
RewriteCond %{REQUESST_URI} !^index\.php$
RewriteRule ^(.*)\.php$ http://www.example.com/$1.html [R=301,L]
RewriteRule ^(.*)\.html$ http://www.example.com/$1.html [R=301,L]

This means i can redirect all the content but leave the Index page intact for me to redirect via other means to the exact place i wanted. Thanks for yours and googles help.

g1smd




msg:4539828
 1:04 pm on Jan 28, 2013 (gmt 0)

Warning! Your second rule doesn't have any conditions!

There are other errors in there. Fixing anchoring, escaping, spelling and then combining and optimising...

RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.co\.uk [NC]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(([^/]+/)*[^/.]+)\.(html|php)$ http://www.example.com/$1.html [R=301,L]

scaggster




msg:4539841
 1:43 pm on Jan 28, 2013 (gmt 0)

Thanks for the reply

On the rewrite url

RewriteRule ^(([^/]+/)*[^/.]+)\.(html|php)$ http://www.example.com/$1.html [R=301,L]

On my site the PHP page redirects arent working now, (i fixed the typo in my htaccess after posting, so i tried this

RewriteRule ^(([^/]+/)*[^/.]+)\.php$ http://www.example.com/$1.php [R=301,L]

Both with and without brackets around the PHP and it doesnt redirect yet using this does

RewriteRule ^(.*)\.php$ http://www.example.com/$1.php [R=301,L]

Whats weird is that for example this wasnt redirecting before (sudomain.example.com/index.php?page=8 to example.com/index.php?page=8) but now it is so thanks massively.

lucy24




msg:4540088
 4:36 am on Jan 29, 2013 (gmt 0)

RewriteRule ^(index)$ example.com/examplepage.php [R=301,L,NC]
Please say that was a typo. Or at least shorthand-for-purposes-of-posting :)

RewriteRule ^(([^/]+/)*[^/.]+)\.php$ http://www.example.com/$1.php [R=301,L]

Both with and without brackets around the PHP and it doesnt redirect yet using this does

RewriteRule ^(.*)\.php$ http://www.example.com/$1.php [R=301,L]


Ugh, ugh. Either we are all overlooking an embarrassingly obvious typo, or something's wrong there. Does the intermediate version redirect? Like this:

^([^.]+)\.php$

(The closing anchor is purely for insurance against malicious and/or malformed requests.)

The form .* is potentially calamitous for various reasons, including hypothetical requests for www.example.com/.php which you would probably not want to redirect.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved