1

while i am new to rewrite I will try to outline this problem in english first than start a thread on how to fix this issue with all your help.

I am trying to remove the folder /blog/ from the following url:

http://blog.site.com/blog/2011/05/26/article-name-test/

with:

http://blog.site.com/2011/05/26/article-name-test/

anubhava
  • 761,203
  • 64
  • 569
  • 643
Chris Hough
  • 3,389
  • 3
  • 41
  • 80

1 Answers1

0

Put this code in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteRule ^blog/?(.*)$ /$1 [R=301,L,NE,NC]

Update: Based on your comments

Here is your suggested .htaccess:

RewriteCond %{HTTP_HOST} ^www\.site\.me$ [NC]
RewriteRule ^ http://site.me%{REQUEST_URI} [R=301,L] 

RewriteCond %{HTTP_HOST} ^holisticho\.me$ [NC]
RewriteRule ^blog/ http://blog.site.me [R=301,L,NC]

Chris Hough Current Edits

Options +FollowSymlinks -MultiViews -Indexes
# ------------------------------------------------------------
# Core rewrite rules
# -----------------------------------------------------------
RewriteEngine on
# -----------------------------------------------------------
# Redirect deleting leading www to root domain if no specified sub is used:
# Allowed Subs: our, test, test.blog, local, local.blog
# -----------------------------------------------------------
RewriteCond %{HTTP_HOST} !^(our|test|test\.blog|local|local\.blog)\.holisticho\.me$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.holisticho\.me$
RewriteRule ^(.*)$ http://holisticho.me/$1 [R=301,L]4
# -----------------------------------------------------------
# Temporary Base Redirect Until Phase One Has been completed
# -----------------------------------------------------------
RewriteCond %{HTTP_HOST} ^holisticho\.me$ [NC]
RewriteRule ^(.*)$ http://our.holisticho.me/$1 [R=301,L]
# -----------------------------------------------------------
# Redirect Any Domains not speficied using /blog/ to the primary url for the blog
# -----------------------------------------------------------
RewriteCond %{HTTP_HOST} ^(test|local)\.holisticho\.me$ [NC]
RewriteCond %{REQUEST_URI} ^/blog/$ [NC] 
RewriteRule (.*) http://our.holisticho.me/ [R=301,L]
# -----------------------------------------------------------
# User can use /login or /admin to log into WP
# -----------------------------------------------------------
RewriteCond %{HTTP_HOST} ^(our|test\.blog|local\.blog)\.holisticho\.me$ [NC]
RewriteRule ^(login|admin)$ http://%{HTTP_HOST}/blog/wp-login.php [NC,L]
# -----------------------------------------------------------
# If the wp-admin redirect is triggered redirect to the log in page with no query string
# -----------------------------------------------------------
RewriteCond %{HTTP_HOST} ^(our|test\.blog|local\.blog)\.holisticho\.me$ [NC]
RewriteCond %{REQUEST_URI} wp-login [NC]
RewriteCond %{QUERY_STRING} redirect_to [NC]
RewriteRule () http://%{HTTP_HOST}/blog/wp-login.php$1?  [R=permanent,NC,L]
# -----------------------------------------------------------
# Add hidden "/blog/" to the url structure 
# -----------------------------------------------------------
RewriteCond %{HTTP_HOST} ^(our|test\.blog|local\.blog)\.holisticho\.me$ [NC]
RewriteRule !^blog/ blog%{REQUEST_URI} [L,NE,NC]
# -----------------------------------------------------------
# Wordpress Permalink formatting
# -----------------------------------------------------------
RewriteCond %{HTTP_HOST} ^(our|test\.blog|local\.blog)\.holisticho\.me$ [NC]
RewriteRule ^index\.php$ - [L,NE,NC]
RewriteCond %{HTTP_HOST} ^(our|test\.blog|local\.blog)\.holisticho\.me$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,NE,NC]
# -----------------------------------------------------------
# Site Wide Error Controllers
# -----------------------------------------------------------
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 402 /402.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
# -----------------------------------------------------------
# Using browser cache: FileETag MTime Size
# -----------------------------------------------------------
<ifmodule mod_expires.c>
 <filesmatch "\.(jpg|gif|png|css|js)$">
  ExpiresActive on
  ExpiresDefault "access plus 1 year"
 </filesmatch>
</ifmodule>
# -----------------------------------------------------------
# Compress static data
# -----------------------------------------------------------
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# -----------------------------------------------------------
# Protect blog from hotlinking
# -----------------------------------------------------------
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?holisticho\.me/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /includes/images/administrative/NoHotlinking.png [L]
# -----------------------------------------------------------
# Fix for infinite loops
# -----------------------------------------------------------
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
# -----------------------------------------------------------
Chris Hough
  • 3,389
  • 3
  • 41
  • 80
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • that removes the blog, but it doesn't appear to work 100% correctly just yet. the home page http://blog.site.com/blog/ does not change to http://blog.site.com/ which may be causing the WP url settings issue. also the WP .htaccess settings are: # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress thank you so much for your help – Chris Hough Jun 02 '11 at 16:30
  • Would you pls give more details like what is not 100%? When I tested on my end a URI of `/blog/2011/05/26/article-name-test/` became `/2011/05/26/article-name-test/` and I thought that's what you wanted. – anubhava Jun 02 '11 at 16:32
  • sorry hit enter too quickly, did the last updated comment help? – Chris Hough Jun 02 '11 at 16:33
  • Oh no problem, I just edited the answer to take care of `/blog` type of URIs as well, pls try again. Also your WP .htaccess might be in the blog directory and this one you should put in the root .htaccess file. – anubhava Jun 02 '11 at 16:35
  • still not working :( here is my .htaccess in the root of the primary domain: – Chris Hough Jun 02 '11 at 16:43
  • Options +FollowSymlinks -MultiViews RewriteEngine on # --------------------------------------------------------------------------- # Core rewrite rules # --------------------------------------------------------------------------- RewriteRule ^blog/?(.*)$ /$1 [R=301,L,NE,NC] # BEGIN WordPress # #RewriteEngine On #RewriteBase / #RewriteRule ^index\.php$ - [L] #RewriteCond %{REQUEST_FILENAME} !-f #RewriteCond %{REQUEST_FILENAME} !-d #RewriteRule . /index.php [L] # # END WordPress – Chris Hough Jun 02 '11 at 16:46
  • let me try to re-explain this a little better if i can try: there are 2 urls pointing to the root site: site1.com and site2.com, i am trying to get site2.com to point to site2.com/blog/ where WP is housed with new /blog/ in the url. so using the first example blog.site.com actually maps to same root as www.site.com but i want all requests coming in as blog.site.com to be pointed to blog.site.com/blog/ where WP is housed and the WP format for the posts should be http://blog.site.com/2011/05/26/article-name-test/ – Chris Hough Jun 02 '11 at 16:46
  • so I am going to have a dashboard landing page setup to point to www.site1.com and blog.site1.com will go to the other segment. sorry for the multiple comments, and thank you for your help, i really appreciate it. – Chris Hough Jun 02 '11 at 16:47
  • i also cleared the .htaccess file from the /blog/ directory as you requested – Chris Hough Jun 02 '11 at 16:50
  • Thanks for your .htaccess, let me recreate this on my end. Also you can actually edit your question and put this in better code format in the question itself rather than here as comment. – anubhava Jun 02 '11 at 16:52
  • Also in your posted .htaccess everything (WP related code) is commented. – anubhava Jun 02 '11 at 16:54
  • correct, i commented it out as a trial in error, it should be uncommented in the final version – Chris Hough Jun 02 '11 at 16:55
  • Even when I un-commented it is working fine for my and removed `/blog` from URI every time. I suspect some other thing going on your installation. For debugging enable RewriteLog in your apache config and see what it tells you. – anubhava Jun 02 '11 at 17:01
  • 2 things i see occurring, the log is not logging errors, but what appears to be happening is the site does not stay in the /blog/ folder – Chris Hough Jun 02 '11 at 19:54
  • so http://blog.site.com/ should actually reside here http://blog.site.com/blog/ and http://blog.site.com/blog/2011/05/26/article-name-test should actually be http://blog.site.com/2011/05/26/article-name-test – Chris Hough Jun 02 '11 at 19:55
  • Do you have DOCUMENT_ROOT for `blog.site.com` set to `old-doc-root/blog` ? – anubhava Jun 02 '11 at 20:03
  • both sites are active in this setup, site.com points to the primary root and blog.site.com points to /blog/ inside of that root – Chris Hough Jun 03 '11 at 01:32
  • @chris hough: I have provided your corrected .htaccess in Update section above. – anubhava Jun 03 '11 at 02:51
  • @anubhava so I posted the full progress update, and I must tell you I am beyond grateful to be working with you on this issue, i am learning a lot in the process. this last post may make quite a bit of sense here. now I need to add in the /blog/ rewriting we discussed earlier so basically our.holisticho.me will actually point to our.holisticho.me/blog/ but I only want to show our.holisticho.me in the url, never showing the "/blog/" folder. if i can get this working i can work on the wordpress rules – Chris Hough Jun 03 '11 at 03:38
  • @anubhava I just updated the full code block making it far easier to read, sorry for the inconvenience of just posted text – Chris Hough Jun 03 '11 at 03:42
  • @chris hough: When I type `http://our.holisticho.me/` in my browser I am getting a 301 redirect to `http://holisticho.me/blog`. Is it being done via another RewriteRule we haven't discussed so far? Also I don't see your uploaded .htaccess code in your question. – anubhava Jun 03 '11 at 03:45
  • @anubhava, now I need to just add back the WP .htaccess settings for posts and I think I am good to go: BEGIN WordPress RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] END WordPress – Chris Hough Jun 03 '11 at 04:07
  • but those rules should only apply to the our.holisticho.me url address – Chris Hough Jun 03 '11 at 04:08
  • To check for only that host you can have a rewrite condition like this: `RewriteCond %{HTTP_HOST} ^our\.holisticho\.me$ [NC]` – anubhava Jun 03 '11 at 04:28
  • i just need to add back the wordpress rewrite rules and it should be good to go – Chris Hough Jun 03 '11 at 04:37
  • Looks good to me, however if for some reason it doesn't work then pls paste your apache error log or enable RewriteLog in your apache config. – anubhava Jun 03 '11 at 04:42
  • @anubhava i wanted to say thank you for all of your help, I was finally able to get this to work – Chris Hough Jun 05 '11 at 04:09
  • You are most welcome. If you can mark this answer as accepted it will close this Q&A and benefit others. – anubhava Jun 05 '11 at 04:43
  • @anubhava i am finalizing it today, but i noticed the code edits I submitted to your post are not displaying is that supposed to happen or am I missing something? just wanted to be sure before closing it :( thanks again – Chris Hough Jun 05 '11 at 20:17
  • @chris hough: You should be able to edit it as much as you want. Not sure if there is some SO restriction based on user's reputation. It could be because you're a new user and not have big reputation number yet. If you send me your edits in comments section (or just submit an edit of your question) I can append that to my answer. – anubhava Jun 05 '11 at 22:44
  • sounds good, after a few days of testing everything appears to be working, with 1 exception, i need it to redirect any sub domain attempt that is invalid i.e. t.holisticho.me to http://holisticho.me – Chris Hough Jun 10 '11 at 02:14
  • @anubhava I just edited your submission with my latest .htaccess, only working on one more fix i hope to correct the sub domain issue previously posted above – Chris Hough Jun 10 '11 at 02:25
  • That's wonderful. Also pls don't forget to mark this answer as "Accepted" when you get a chance to close this Q&A. – anubhava Jun 10 '11 at 02:28
  • fixed and good to go :) thank you soo much for your help, seriously man, I really appreciate it. – Chris Hough Jun 10 '11 at 02:33
  • btw, its waiting for your peer review to allow my edit to go through :) – Chris Hough Jun 10 '11 at 02:34
  • @Chris: You are most welcome, its been really a pleasure to work through this problem with you and frankly speaking it was due to more of your hard work that we made it work. Also I approved pending edit and your edits are now visible in the answer above. – anubhava Jun 10 '11 at 02:41
  • @anubhava your too kind bro, I really hope you have a wonderful rest of the week :) – Chris Hough Jun 10 '11 at 02:45