Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1313
  • Last Modified:

.htaccess redirect 301 with exceptions, help please

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
Fahdmurtaza
Asked:
Fahdmurtaza
  • 5
  • 4
2 Solutions
 
David S.Commented:
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
 
FahdmurtazaAuthor Commented:
doesn't work

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

http://yogicameron.com/

0
 
FahdmurtazaAuthor Commented:
Did I say thanks for helping me :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
David S.Commented:
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
 
caterham_wwwCommented:
> 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
 
FahdmurtazaAuthor Commented:
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
 
David S.Commented:
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
 
FahdmurtazaAuthor Commented:
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
 
FahdmurtazaAuthor Commented:
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
 
David S.Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now