Is this URL masking?

For the sake of organisation I would like to keep all my content in one directory on the server

http://domain.com/content/apple/
http://domain.com/content/banana/

but I would like the browser to display

http://domain.com/apple/
http://domain.com/banana/

Is this possible?

Sandy
LVL 2
SandyCookeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

caterham_wwwCommented:
If you call /banana/ you should get the page from /content/banana/, so it should not be visible that your pages served from /content/? That can be done with mod_rewrite in a .htaccess file

RewriteEngine on
# rewrite a request of /apple/ --internally to--> /content/apple/
RewriteRule ^([^/.]+)/$ /content/$1 [L]

I'm not sure, if the regular expression fits all your needs, bot it would match on your example.
0
SandyCookeAuthor Commented:
Thanks for the help but with the above code a request for

http://domain.com/apple/

is redirecting to

http://domain.com/content/apple/

but the browser address is also changing to

http://domain.com/content/apple/

which I would like to avoid...

?

p.s. does # denote a comment in Apache? I am a tru newb!
0
caterham_wwwCommented:
> but the browser address is also changing to

That is not normal behaviour. Only if you add the R-Flag or prefix the substitution with scheme+host, an external redirection would occour from mod_rewrite. Do you have the RedirctMatch in place? That would/might cause an additional problem, too.

RewriteEngine on
# the same RedirectMatch does
RewriteCond %{THE_REQUEST} /content/
RewriteRule ^content/(.*) /$1 [R=301,L]
# rewrite a request of /apple/ --internally to--> /content/apple/
RewriteRule ^([^/.]+)/$ /content/$1 [L]

If there's still a redirect, the only way to analyzing it would be a rewrite log, because this is not normal behaviour.

> does # denote a comment in Apache?

yes.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

SandyCookeAuthor Commented:
@ caterham_www

I am trying to combine the fruits of your recent help into one .htaccess file

This works as desired:

----------------------------------------------------------

# legacy redirect:

RedirectMatch 301 ^/legacydir/(.*) http://mydomain.com/$1

RewriteEngine on

# add trailing slash to avoid browser adding "www"

RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,5}$ - [C]
RewriteRule ^(.+) http://%{HTTP_HOST}/$1/ [R=301,L]

------------------------------------------------------

BUT

I would additionally like a request for

http://domain.com/banana/

to "secretly" serve the content of

http://domain.com/fruit/banana/

SO

I have added

RewriteRule ^([^/.]+)/$ /fruit/$1 [L]

at the bottom of the file

BUT

this just redirects as mentioned above (the browser address changes)

SO

I replaced this line with

# RewriteEngine already on
RewriteCond %{THE_REQUEST} /fruit/
RewriteRule ^ fruit/(.*) /$1 [R=301,L]
RewriteRule ^([^/.]+)/$ /fruit/$1 [L]

and I get a 500 error presumably because (if I understand you correctly) the RedirectMatch at the top of the file is interfering with this bit

RewriteCond %{THE_REQUEST} /fruit/
RewriteRule ^ fruit/(.*) /$1 [R=301,L]

?

0
caterham_wwwCommented:
RewriteRule ^ fruit/(.*) /$1 [R=301,L]
             ^^^^^
there is a space where no space should be: between ^  and fruit/

^fruit/


> the RedirectMatch at the top of the file is interfering with this bit

Those two lines are a replacement for RedirectMatch
0
SandyCookeAuthor Commented:
Still no joy - when I request

http://domain.com/banana/

Safari gives me the error:

Too many redirects occurred trying to open “http://www.domain.com/fruit/banana/”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

The whole .htaccess file is below:

--------------------------------------

# legacy redirect

RedirectMatch 301 ^/legacydir/(.*) http://domain.com/$1

RewriteEngine on

# add trailing slash to request if missing to avoid browser adding "www"

RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,5}$ - [C]
RewriteRule ^(.+) http://%{HTTP_HOST}/$1/ [R=301,L]

# internally serve request for http://domain.com/banana/ with content from directory http://domain.com/fruit/banana/

# RewriteEngine already on
RewriteCond %{THE_REQUEST} /fruit/
RewriteRule ^fruit/(.*) /$1 [R=301,L]
RewriteRule ^([^/.]+)/$ /fruit/$1 [L]
0
caterham_wwwCommented:
Ah, I see the problem. The last line must be

RewriteRule ^([^/.]+/)$ /fruit/$1 [L]
0
SandyCookeAuthor Commented:
Thank you for your tireless assistance but now a request for

http://domain.com/banana/

is correctly loading the index page of

http://domain.com/fruit/banana/

but all the links in that index page

<IMG SRC="banana.jpg" etc.

are Not Found because the browser is trying to load

http://domain.com/banana/banana.jpg

while the image is actually at

http://domain.com/fruit/banana/banana.jpg

?
0
caterham_wwwCommented:
two ways to resolve it: instead of the last rule:

RewriteCond $1 !^fruit
RewriteRule ^([^/.]+/.*)$ /fruit/$1 [L]

to rewrite all other ressources, too.
Or you're using absolute paths src="/fruit/banana/banana.jpg"
0
SandyCookeAuthor Commented:
Is it possible to exclude certain requests from this rule?

For instance I would like a request for

http://domain.com/myxmlfile.xml

to load

http://domain.com/myxmlfile.xml

not

http://domain.com/fruit/myxmlfile.xml

?

P.S. When you say "instead of the last rule" does "the last rule" mean all of

RewriteCond %{THE_REQUEST} /fruit/
RewriteRule ^fruit/(.*) /$1 [R=301,L]
RewriteRule ^([^/.]+)/$ /fruit/$1 [L]
0
caterham_wwwCommented:
> P.S. When you say "instead of the last rule" does "the last rule" mean all of

no, only the last line.

> Is it possible to exclude certain requests from this rule?

Yes, but /myxmlfile.xml should not be affected by the rules, only /folder/myxmlfile.xml would be affected. The RegEx would match only, if a folder (slash) is present.
To exclude other folders

RewriteCond $1 !^(fruit|other-folder)
RewriteRule ^([^/.]+/.*)$ /fruit/$1 [L]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SandyCookeAuthor Commented:
You are a star! Thank you for your patience, it works like a charm!
0
SandyCookeAuthor Commented:
p.s. the fruits of your labour are at

http://golo.co.uk/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.