Solved

.htaccess redirect 301 with exceptions, help please

Posted on 2009-05-12
10
1,300 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now