.htaccess redirect certain virtual directories - .htaccess

/podcast/wp/ is a folder, everything else is a virtual directory already generated by RewriteEngine. Here's the code provided by WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /podcast/wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /podcast/wp/index.php [L]
</IfModule>
I would like to redirect all requests in the wp/ directory (except for existing folders)
excluding the following possible paths (also virtual directories):
/podcast/wp/ANYSTRING1/ANYSTRING2/feed
to another domain:
example.com
using .htaccess while the excluded path remains working as is.
The goal is to "hide" (redirect) the entire WordPress blog except for the feeds.
Thanks for your help!

Change the wordpress generated rules to:
RewriteEngine On
RewriteBase /podcast/wp/
# new stuff
RewriteCond %{REQUEST_URI} ![^/]+/[^/]+/feed$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/ [L,R]
# original wordpress stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /podcast/wp/index.php [L]
Depending on how you want to handle the redirect, you can tweak the rule that redirects to http://example.com/. If you want 301 permanent redirects, add a 301:
RewriteRule ^(.*)$ http://example.com/ [L,R=301]
If you want to preserve the relative URI in the redirect, use a backreference:
RewriteRule ^(.*)$ http://example.com/$1 [L,R]
If you want to preserve the entire URI (including the /podcast/wp/ part, use the URI:
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [L,R]

Related

Allowing a trailing slash for a specific dynamic url?

I have done a 301 redirect for all trailing splash URLs in my htaccess file. Now I have a few dynamic URLs `website.com/item/activate/#dynamiclychangingvalue**/** that need the splash. Note that in the end on the activate folder is a dynamic url that needs the splash.
How would I edit the .htaccess file? I have tried all sorts of codes, they dont seem to be working.
IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
the dynamic url looks like this website.com/item/activate/#dynamiclychangingvalue**/**
You can use:
RewriteCond %{REQUEST_URI} !^/additem/ [NC]
RewriteRule ^(.+)/$ $1 [R=301,L]

How can I hide folder name from URL using .htaccess

I have a website with the root folder 'www', but I put all php files including index.php in a sub-folder of root.
I wrote myself a .htaccess file to redirect, so if I input www.test.com, it will jump to www.test.com/folder and display the index.php.
Below it's my .htaccess which I put in the root.
RewriteEngine On
RewriteBase /
RewriteRule ^folder2 - [L]
#ignore folder2 in which I put important files, but no works for the website
RewriteCond %{REQUEST_URI} !^/folder(.*)
RewriteRule (.*) /folder/$1
Right now, I want to change the .htaccess file to reach these goals:
When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
All the pages in folder will display the url without folder name.
Such as
www.test.com/shop/page1 -> www.test.com/page1
I searched some of the scripts, but none of them works.
I've found this related question which answers to your problem. The snippet from the answer is (note: it's adjusted to your needs):
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
Reference: https://stackoverflow.com/a/18361995/3673491
You can use below code.
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]
Based on your current rules. You can just make a slight adjustment.
RewriteEngine On
RewriteBase /
#ignore folder2 in which I put important files, but no works for the website
RewriteRule ^folder2/? - [L]
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]

htaccess removing folder from url externally and redirecting internally

I manged to remove the folder from the url but I cannot redirect back to it. Please help!
# externally redirect /dir/foo to /foo
RewriteCond %{THE_REQUEST} pages/
RewriteRule pages/(.*)$ $1 [L,NC,R]
# internally forward /foo to /dir/foo
#what goes here?
EDIT:
Also, how can I put this on the same code?
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ profile.php?username=$1 [L]
I want the user name to be like this localhost/test/user1 rather than localhost/test/profile.php?username=user1
Internal redirect mean URL rewriting (if I'm not misunderstood)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /dir/$1 [L]
Note: To prevent external redirect I didn't used [R] flag.
EDIT
I can see you hostead on localhost in subdirectory /test So, you'l have to set RewriteBase as following:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /test/dir/$1 [L]
EDIT Included user profile rewrite rule
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/profile.php?username=$1 [L]
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /test/dir/$1 [L]

Redirecting domain with htaccess

I'm moving my site from the .com version to the .co.uk. The only issue is there are some admin facilities that I want to keep on the .com site. I've set up my .htaccess to redirect everything but requested files and directories that exist which works fine, except for the root of the site, I can't get that to redirect.
Here is my htaccess...
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ http://www.site.co.uk/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.site.co.uk/$1 [R=301,L]
You can use this code:
RewriteEngine On
RewriteBase /
RewriteRule ^(index\.php)?$ http://www.site.co.uk/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.site.co.uk/$1 [R=301,L]
This regex ^(index\.php)?$ will match index.php OR empty string (landing page).

.htaccess redirect woes — not completing

So, at the moment, my .htaccess looks a little like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ "http\:\/\/i\.colorspace\.am\/portfolio$1" [R=301,L]
I want to move all the content from my root directory into a subdomain (which I've done) but I don't want the links people have to not work. www.colorspace.am/portfolio needs to redirect to i.colorspace.am/portfolio, and all the sets contained therein (ie /portfolio/YYYYMMDD) also need to be 'adjusted' on the fly (www.colorspace.am/portfolio/YYYYMMDD -> i.colorspace.am/porfolio/YYYYMMDD
NOTE: i.colorspace.am contain's 2011's content; ii.colorspace.am will contain 2012. They're two entirely different WP installs with their own respective databases. Not sure if it's relevant but..
What seems to be happening is that /portfolio is instructed to redirect to i.colorspace.am/portfolio, but for whatever reason it's ending up at i.colorspace.am
IF there's a way I can make any www.colorspace.am/folder/sub-folder redirect to i.colorspace.am/folder/sub-folder (wildcard?) ..that would be amazing. But I'd be just as happy with a single fully working redirect at this point.
NOTE: the redirect code was generated by my administration panel. I tried
Redirect /portfolio http://i.colorspace.am/portfolio
But it resulted in a 'too many redirects' error.
After the redirection from
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
The rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Is accessed again. That is the reason it's ending up at i.colorspace.am.
Add these 2 rules
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
in order mentioned below.
also change (just a small optimization)
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
to
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
and put it inside of IfModule block in the same order mentioned below.
Have just the below in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#add these 2 lines:
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
</IfModule>
# END WordPress

Resources