Solved

.htaccess redirect 301 with exceptions, help please

Posted on 2009-05-12
10
1,306 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 42

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 42

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 135 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 42

Assisted Solution

by:David S.
David S. earned 100 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 42

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

729 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