Solved

.htaccess redirect 301 with exceptions, help please

Posted on 2009-05-12
10
1,307 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

617 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