Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.htaccess redirect 301 with exceptions, help please

Posted on 2009-05-12
10
Medium Priority
?
1,309 Views
Last Modified: 2012-05-06
Hi

I have moved my wordpress into a subdirectory, it was on root before

the directory's name is articles and its under root. I have moved successfully and got my new permalinks working fine. So the old urls now are 301 redirected to contain the /articles/ within url

For example

http://yogicameron.com/ayurveda1/i-fasted-for-the-first-time-the-other-day-ask-a-yogi/

is successfully redirecting permanetly to

http://yogicameron.com/articles/ayurveda1/i-fasted-for-the-first-time-the-other-day-ask-a-yogi/

now. So that part is 100% OK.

Now here is the tricky part. I don't want to do permanent redirection on domain name itself

Let me explain

http://yogicameron.com/

redirects to

http://yogicameron.com/articles/

while I dont want to have permanent direction on the domain itself. Rather I want the index.html to be accessed when http://yogicameron.com/ is accessed.

Also the last thing, I have a bunch of .html files in the root. I don't want to apply 301 redirection when such a url is accessed

http://yogicameron.com/any_file_name_contaning_and-or_numbers.html 

These html files are in root.

I am attaching the code for .htaccess in my root. It does what I have specified above and lacks the functionality I mentioned. I am very new to regular expressions, thats why heavily counting on your help.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yogicameron\.com)
RewriteRule ^(.*)$ http://yogicameron.com/articles/$1 [R=permanent,L]

Open in new window

0
Comment
Question by:Fahdmurtaza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 43

Expert Comment

by:David S.
ID: 24370153
Try this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yogicameron\.com
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.*)$ http://yogicameron.com/articles/$1 [R=permanent,L]

Open in new window

0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 24370179
doesn't work

see for yourself, I just uploaded your code as .htaccess in root

http://yogicameron.com/

0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 24370185
Did I say thanks for helping me :)
0
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

 
LVL 43

Expert Comment

by:David S.
ID: 24370215
Hmm... try this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yogicameron\.com
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^(.*)$ http://yogicameron.com/articles/$1 [R=permanent,L]

Open in new window

0
 
LVL 27

Accepted Solution

by:
caterham_www earned 540 total points
ID: 24370247
> Also the last thing, I have a bunch of .html files in the root. I don't want to apply 301 redirection when such a url is accessed

So you'd like to redirect /folder/foo to /articles/folder/foo
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} =yogicameron.com
RewriteRule ^([^/]+/.*)$ http://yogicameron.com/articles/$1 [R=permanent,L]
 
 
 
^([^/]+/.*)$ =
^start of string
[^/]+ 1 or n characters not a slash (negated character class)
/ followed by a slash
.* followed by 0 or n characters
the match is captured in a backreference no#1 (by grouping using the parenthesis)

Open in new window

0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 24370254
see for yourself. I just uploaded. Only one change

http://yogicameron.com/

now points to index.html but messes up the CSS.

And the html files still not working.
0
 
LVL 43

Assisted Solution

by:David S.
David S. earned 400 total points
ID: 24370278
Ah. It's redirecting the URL for the stylesheet too. Which is what should happen until another exception is added:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yogicameron\.com
RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{REQUEST_URI} !\.html$
RewriteCond %{REQUEST_URI} !\.(css|js|gif|png|jpg|jpeg|swf)$
RewriteRule ^(.*)$ http://yogicameron.com/articles/$1 [R=permanent,L]

Open in new window

0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 24370298
Thanks Kravimir, the solution from caterham_www seems to be perfect. I think it also solved the CSS issue, I think its Javascript file not accessible which makes the menu to appear this way. I am testing it. Will be back in a few with updates. I really appreciate you both guys helping me.
0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 24370332
Bravo guys

Not only you taught me new things. But I ended up doing it quickly. I love EE. Its always full of real experts. I miss the days I was a daily contributor but my job timings don't allow me to do so :(.

Once again, I am really grateful. Please guide me what should I do to learn these tricky things. I am a professional programmer but I have always been lazy to learn these regular expression things. So please guide me about any book, online good resource where I can start from.


So I added for javascript as I figured out from Kravimir's code for exception about css, added js as well and it did all the trick.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} =yogicameron.com
RewriteCond %{REQUEST_URI} !\.(css|js|gif|png|jpg|jpeg|swf|js)$
RewriteRule ^([^/]+/.*)$ http://yogicameron.com/articles/$1 [R=permanent,L]
 

Open in new window

0
 
LVL 43

Expert Comment

by:David S.
ID: 24371212
You're welcome.

> So I added for javascript as I figured out from Kravimir's code for exception about css, added js as well
> and it did all the trick.

I already added that. It's the second extension in the list.


> Please guide me what should I do to learn these tricky things.

It really depends on which languages you intend to use them with, because implementations of them vary from language to language.

The best general resource that I'm aware of is http://www.regular-expressions.info/
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question