Forum Moderators: phranque

Message Too Old, No Replies

I don't speak Apache!

Just not gettin' it at all

         

moggi1964

6:37 pm on Apr 24, 2009 (gmt 0)

10+ Year Member



I have searched the web and this forum and I am absolutely certain that the information I need has passed through my virtual fingers a 100 times. The only problem is that the words may as well be in Ancient Greek because i just don't know when I have found the right information.

I am hoping someone here can either talk me through the changes or even just write the code I need which I think will only be a couple of lines.

I do practice and believe in the 'search before you ask' mantra but I am blindfolded and lost in the woods; HELP!

Here is what I need to do:

I have built a cart for my website and it is in a subdirectory one level down. I used a redirect so that when someone types in example.com they get taken to example.com/zencart. What doesn't happen is the text in the browser still shows example.com/zencart and I don't want it to show the zencart part.

How do I make that happen please?

Thanks in advance.

[edited by: jdMorgan at 6:20 am (utc) on April 26, 2009]
[edit reason] Please use example.com [/edit]

g1smd

6:51 pm on Apr 24, 2009 (gmt 0)

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



To make that happen, either change the public root of the website (as defined in the control panel) to be that folder, OR install a rewrite (not a redirect) so that when URLs at '/' are requested, the files at '/zencart/' are fetched. A rewrite does that silently. A redirect exposes the workings and is the wrong thing to use.

moggi1964

7:47 pm on Apr 24, 2009 (gmt 0)

10+ Year Member



And the rewrite would be in a htaccess file at the root level?

And now the fun bit:

what would the rewrite say exactly? I tried writing one but it kept failing.

Thank you.

g1smd

9:05 am on Apr 25, 2009 (gmt 0)

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



You'll need a RewriteRule with the [L] flag. The [R] flag must NOT be used.

The pattern will need to match the format of the path part of valid URL requests, and the target will specify the location of those files inside the server as a path.

RewriteRule <pattern> <target> [L]

You must not rewrite all requests, because you do not wnat to rewrite requests for robots.txt and other such files.

moggi1964

8:55 pm on Apr 25, 2009 (gmt 0)

10+ Year Member



g1smd,

I feel like the dog in one of those Far Side cards. The guy is saying something to the dog and the dog hears:

"blah; blah; blah; blah; Rover; blah. blah. blah."

In other words, you lost me ;) I am easily lost!

I wish I spoke the language.

moggi1964

8:58 pm on Apr 25, 2009 (gmt 0)

10+ Year Member



Would this work or am I totally off base? I copied this from some other discussion.

RewriteEngine on
rewritecond %{http_host} ^www.example.com [nc]
rewriterule ^(.*)$ http://www.example.com/zencart $1 [L=301,nc]

[edited by: jdMorgan at 6:26 am (utc) on April 26, 2009]
[edit reason] example.com [/edit]

g1smd

9:17 pm on Apr 25, 2009 (gmt 0)

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



This will be a problem, because by editing the configuration you are making fundamental changes to the way your webserver works. An error here could stop your site working or silently destroy your listings in search engines. You do need to understand at least the basics, to avoid these dangers. Always try code out on a non-public development server and test it thoroughly before adding it to a live site.

To your question, and example code...
- Restricting your code to the www version in the RewriteCond would mean the rewrite would not operate for the non-www version of your site. That will be a major problem.
- The ^(.*)$ part can be simplified to (.*) here. Use the [L] flag; the '301' part is not required.
- In a rewrite, you must not specify a domain name in the target. Adding a domain name makes it a redirect, not a rewrite.
- You also need to fix requests as being one of either www or non-www, by redirecting the other one to the canonical form.

.

Try something like:

# Redirect non-www URLs to www, retaining same path part.
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Rewrite root URL requests to the /zencart/ folder.
RewriteCond $1 !^zencart
RewriteRule (.*) /zencart/$1 [L]

unless I have also overlooked something.

Oh, and you will need to place your robots.txt and search engine verification files into the /zencart/ folder too - because nothing in the old root folder will be working any more. That is, the physical /zencart/ folder on the server is now seen as being the root of the site when viewed from the web.

moggi1964

2:16 am on Apr 26, 2009 (gmt 0)

10+ Year Member



Thank you; I will try that.

g1smd

10:00 am on Apr 26, 2009 (gmt 0)

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



Note that all your content can still be accessed as a Duplicate if you include /zencart/ in the requested URL, so ahead of all the other code you will need a redirect to stop that happening:

# Redirect /zencart/ URL requests to www.example.com/ 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\ /zencart/
RewiteRule ^zencart/(.*) http://www.example.com/$1 [R=301,L]

The above code goes ahead of the other code you have already.

Do test out what happens when you request exactly

example.com/zencar[b]t[/b]
and be sure that it does redirect too.

moggi1964

5:05 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



Okay, here is what I ahve in my .htaccess which is on the root folder

# Redirect /zencart/ URL requests to www.apmamerica.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\ /zencart/
RewiteRule ^zencart/(.*) [apmamerica.com.com...] [R=301,L]
# Redirect non-www URLs to www, retaining same path part.
RewriteCond %{HTTP_HOST} !^www\.ampamerica\.com$
RewriteRule (.*) [apmamerica.com...] [R=301,L]
# Rewrite root URL requests to the /zencart/ folder.
RewriteCond $1 !^zencart
RewriteRule (.*) /zencart/$1 [L]

So, no matter what variation I type in I get to my site except I still have it displaying the zencart suffix on the address in the browser.

So, thanks to you g1smd, I am part of the way there. What do I need to do to get the brower bar to say apmamerica.com instead of apmamerica.com/zencart?

Also, the bar does not show the WWW on any approach now; that is fine right?

Thanks.

g1smd

5:22 pm on Apr 26, 2009 (gmt 0)

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



The code above forces the www, so if it isn't there, then you have some other rule at work doing that.

The code above removes the /zencart/ part, so if it is being added back on, then you have some other rule at work doing that.

Use the Live HTTP Headers extension for Firefox, to see what actually happens. There should be zero or one redirect for any request that you make. For any request that shows two or more redirects there must be some other rule, elsewhere, doing that.

moggi1964

5:24 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



So, I have gone back to where I started until I hear from someone (probably you g1smd) as I want to be sure the site is running.

I tried deleting the root .htaccess file and cpanel wouldn't let me so i overwrote it with a blank .htaccess.

I think I am pretty close to what I need and am grateful for your continuing assistance.

Found a way to delete the files but now it just gets into a loop according to Firefox - yikes!

g1smd

5:38 pm on Apr 26, 2009 (gmt 0)

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




Use the Live HTTP Headers extension for Firefox, to see what actually happens. There should be zero or one redirect for any request that you make. For any request that shows two or more redirects there must be some other rule, elsewhere, doing that.

Only you can check that out. You need to do that for www and for non-www and do that for / and for /zencart/ for each.

moggi1964

5:55 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



Gotcha!

My host just got back to me saying he could just either move all the content of ZENCART up a level to the root OR put in a change in the root configuration to automatically take people to the correct place and show that as the root (apmamerica.com).

I guess this is what I am trying to achieve so I will let them have a go and we will see what happens. I will let you know.

IT WORKED!

Thanks for your help g1smd; I am very appreciative.

g1smd

6:11 pm on Apr 26, 2009 (gmt 0)

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



They have basically implemented part of the above code into httpd.conf instead of your local .htaccess file.

You need to check what of the other parts you still need to retain in the .htaccess file: the non-www to www redirect is likely one of them.

moggi1964

6:49 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



Thank you, I will check that.