htaccss redirection - .htaccess

I just moved a site to a subdirectory. Instead of changing every link I'd like to do a 301 redirect via htaccess. Heres an example of what I want to do:
When someone tries to go to www.example.com/test.html, I want them really to go to www.example.com/website/test.html. When someone goes to www.example.com/documents/test.pdf, I want them to go to www.example.com/website/documents/test.pdf.
I want to append the /website/ directory to every request basically. Because I moved the site from the root folder into this subdirectory.
I do have one restriction, I want to make it so that www.example.com/website1/ still goes to www.example.com/website1/. If this changes the solution please give me both solutions, because I am flexible with this.

2 basic approaches (there are some more, but they depend on your rewrite logic (other rules) so may not be suitable for every scenario):
First:
# Redirect all incoming requests into /website/ subfolder
# but excluding /website1/ folder
# First condition is to prevent infinite redirect
RewriteCond %{REQUEST_URI} !^/website/
RewriteCond %{REQUEST_URI} !^/website1/
RewriteRule ^.*$ http://www.example.com/website%{REQUEST_URI} [R=301,L]
Second:
# Redirect all incoming requests into /website/ subfolder
# but excluding /website1/ folder
# Condition is to prevent infinite redirect
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(?!website1/).*$ http://www.example.com/website%{REQUEST_URI} [R=301,L]

Related

htaccess rewrite rule permanently redirects certain pages, even though I don't want it to

I like to deploy little and often, so my deploy script creates a dated folder in the docroot (e.g. /202206281019/), and I update my .htaccess file to point to it.
There is another folder for images (/static/), as they rarely get updated and so I don't want to deploy them every time. That is also referenced in the .htaccess.
- public_html/
|
-- .htaccess
-- 202206281019/
-- static/
In both of these cases, I'm re-writing internally, so that the visitor does not know anything about my site structure.
This works exactly as I expect across most of the site, except for one section where the rule appears to send a 301 redirect message back to the browser.
I really, really don't want this, and do not understand why it is happening in only one section of the site.
I'd be really grateful for another pair of eyes checking my work ...
Options -Indexes
DirectoryIndex index.html
<IfModule mod_rewrite.c>
RewriteEngine on
# /wp-content only holds images/styles/scripts
RewriteCond %{REQUEST_URI} ^/wp-content/
RewriteRule ^(.*)$ /static/$1 [NC,END]
# /wp-includes only holds images/styles/scripts
RewriteCond %{REQUEST_URI} ^/wp-includes/
RewriteRule ^(.*)$ /static/$1 [NC,END]
# everything else gets re-written
RewriteCond %{REQUEST_URI} !^/202206281019
RewriteRule ^(.*)$ /202206281019/$1 [NC,L]
</IfModule>
My staging site: https://staging.achaneich.co.uk/uk-clubs-directory/. As you click around the site, all requests are silently rewritten as I want. However, if you click on a link to a club (e.g. https://staging.achaneich.co.uk/uk-clubs-directory/name/lochaber-aikido), that gets redirected (to https://staging.achaneich.co.uk/202206281019/uk-clubs-directory/name/lochaber-aikido/). The image rewrite works as expected throughout the site.
Thanks in advance for your thoughts!

.htaccess root url to subfolder, and specific url to root folder

My setup to weird, needless to say im particularly limited in terms of what i can change. At the moment, the easiest thing for me to change is the server's .htaccess file. The current directory setup looks like:
/ is a wordpress installation
/front-end is a react website that uses the wordpress installations REST API to get data
I am trying to get it so that when i go to example.com it serves content from /front-end but all requests to /dashboard are served from the root directory.
For example these URLs would do the following:
/ and /contact would point to the index.html file inside the front-end directory (react-router would pick up the /contact)
/dashboard/wp-admin and /dashboard/wp-json/menus would let wordpress in the route directory take over. (these would ALWAYS start /dashboard)
Is what im trying to achieve doable?
I have this to start with, but im unsure how to ignore the /dashboard route
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/front-end/%{REQUEST_URI} -s
RewriteRule . /front-end/index.html [L]
RewriteEngine On
RewriteRule ^/?dashboard/(.*)$ /$1?wordpress=true [L,NC]
RewriteCond %{QUERY_STRING} !^.*wordpress=true.*$ [NC]
RewriteCond %{REQUEST_URI} !^/?front\-end.*$ [NC]
RewriteRule ^(.*)$ front-end/$1 [L,NC]
In the above rules, we first check if we have dashboard in our requests. If yes, we deliberately attach a query string wordpress=true to avoid confusion with your react folder and run into a too many redirects issue.
This way, we also know that the current request is for wordpress and further rewrite rules should not touch it.
In the second rewrite rule, we check the conditions as to whether we have wordpress=true or not and see if this does not already have front-end in the URL(also applicable for internal routing and need not be explicitly specified). If it passed both tests, we redirect it to your react front-end, else we leave it to execute as is.
Reason to attach !^/?front\-end.*$ is to avoid infinite internal redirects to itself.

Rewrite URL and remove part with .htaccess

I am really stuck with my .htaccess file, and need some help :). I have a WordPress installation that I am using for testing. It is in a folder and I use .htaccess to get there. This is the rules I use so far:
######### Custom #########
RewriteEngine On
# ignore folders
RewriteCond %{REQUEST_URI} "/af1wp/"
RewriteRule (.*) $1 [L]
###############
# only for me #
###############
# HOME (Senne Tijdeman)
RewriteCond %{REMOTE_ADDR} ^###\.###\.###\.###$
RewriteCond %{HTTP_HOST} ^((www.)?([a-z0-9_\-]+).)?alleenf1.nl$
RewriteCond %{REQUEST_URI} !^/af1wp/$
RewriteRule ^(.*)$ /af1wp/$1 [L]
This works (with my real IP address of course), so no problem there. But now I want to rewrite exisiting URL's to a new format. The old URL is this:
http://alleenf1.nl/nieuws/QOgbb/raikkonen-alles-is-mogelijk-in-australi
The new URL should be this:
http://alleenf1.nl/raikkonen-alles-is-mogelijk-in-australi
The part I want to remove "nieuws/QOgbb/" is not always the same, so I have to use regex for that. But everything I tried did not work at all.
I thought this would be simple, but apparently not for me unfortunately. Now I have 2 questions.
What is the right RewriteRule to do this?
Where should I put it. In the .htaccess of the root folder, or the af1wp folder where the WordPress install is?
Tnx in advanced
To awnser the questions from poncha below:
Yes, the URL's always start with to folders. Just to clarify (was not clear) the part "nieuws" is always the same, but not the second part (call it an ID).
I prefer a redirect.
The file /raikkonen-alles-is-mogelijk-in-australi is a post in WordPress. That WordPress installation currently resides in the folder af1wp, but will be moved to the root folder when going live.
Try this:
RewriteEngine On
RewriteRule ^nieuws/([^/]+)/(.*) /af1wp/$1 [R=301,L,QSA]
This will only match URLs starting with "nieuws"
For now, the rewrite target is /af1wp/, change it to / when moving the wordpress.
When you move wordpress, you'll need to mix in this rule inside the wordpress rules, as it already has rewrite rules of its own - place this rule above its rules.
The flags used here:
R=301 - redirect with HTTP status 301 (Moved Permanently).
L - last rule (stop rules parsing after successful match of this rule)
QSA - query-string-append (append original query string to the rewritten request).

.htaccess, proper rewriting of directory and file with same name

As of now my website has a few static pages, one of which is /portfolio. Among other things, my htaccess hides the .html extension. I'd like to add a portfolio directory, but I do not want to move my existing portfolio page into the portfolio directory as the default index file. My /portfolio page is one of my Google sitelinks and I am afraid if it is moved or if the url changes in someway, Google will consider it to be a brand new page.
My problem is once I add the /portfolio/ directory, whenever I try to visit the original /portfolio page, a trailing slash is automatically added and it links to the directory itself.
I've tried countless options, one being a rewrite of /portfolio/ to /portfolio, however this creates an infinite loop. I also tried "DirectorySlash Off" but that only removed the trailing slash while being inside the directory, it didn't revert access to the original /portfolio page.
Ultimately, I would like to keep my /portfolio page as-is, linking to pages inside the directory like so /portfolio/example and if either /portfolio or /portfolio/ is accessed it will result in showing the same page which is outside of the directory without Google thinking it is duplicate content.
A similar question exists here:
.htaccess rewriting url to page or directory though this still resulted in an infinite loop for me for some reason, I'm guess it has something to do with the hidden extensions.
Here's my htaccess-
RewriteEngine On
# HTML to PHP
RemoveHandler .html .htm
AddType application/x-httpd-php .htm .html
# Hide extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Force WWW
RewriteCond %{HTTP_HOST} ^mydomain\.net
RewriteRule ^(.*)$ http://www.mydomain.net/$1 [R=301,L]
# Blog Subdomain
RewriteCond %{HTTP_HOST} ^blog.mydomain.net$
RewriteRule ^(.*)$ http://www.mydomain.net/blog/$1 [R=301,L]
I know it's not a great idea having a directory with the same name as a static page, but I really would rather not alter the existing page and lose the Google sitelink, so a clean and proper way to handle this would be a help.
There are two things going "wrong" here, and two ways to fix it.
The first is that apache "figures out" that there is a directory by the name of "portfolio" before the rewrite conditions are applied. That means that the rewrite conditions are receiving "portfolio/" instead of "portfolio".
Second, the "!-d" rule is specifically avoiding the rewrite that you want to make if there is in fact a directory by that name
Solution 1: Manually re-route requests for the portfolio directory to remove the slash.
# Manually re-route portfolio/ requests to portfolio
RewriteCond %{REQUEST_FILENAME} portfolio/$
RewriteRule ^(.*)/$ $1
# Hide extension
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Note the removal of the "!-d" condition.
The downside to this is that you are having to hard-code the "portfolio" edge case directly into the rewrite rules, and will still result in the browser being first redirected to portfolio/
Solution 2: Set DirectorySlash Off and remove directory exists test
# Disable Automatic Directory detection
DirectorySlash Off
# Hide extension
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Setting DirectorySlash Off would fix this issue the best, but may break other parts of your site where you actually want the auto DirectorySlash. Best of Luck, and I hope this helps.
Note when testing solution 2, your browser may remember the redirect of "portfolio" to "portfolio/" and perform the redirect before it even sends the request to the server. Be sure to test in a cache-clear, clean environment for best results.

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources