htaccess and redirection to a holding page - .htaccess

I'd like to redirect all outside visitors to a holding page whilst allowing all internal users to see the whole site.
I have the following
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.1.1.1
RewriteCond %{REQUEST_URI} !/holding-page/index.php$
RewriteRule .* /holding-page/index.php [R=302,L]
Which does what I want but the holding page won't pull through any styling or images.
Does anyone know who I'd achieve this?

That is due to the use of relative links on your holding page.
Insert one more rule to fix that:
RewriteRule ^holding/(.+?\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [L,NC,R=301]
If that doesn't fix the problem you can adapt these:
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can add this in your page's HTML header: <base href="/" />

I managed to resolve by allowing access to the /holding-page/ directory with the following rule
RewriteCond %{REQUEST_URI} !/holding-page/

Related

htaccess | not looking in the right location for js

I am trying to write a htaccess file that will allow for /example/1/profile to look for a JavaScript file within /example/. Currently on Internet Explorer 11 it is looking for /example/1/file.js whereas realistically it should be looking for /example/file.js.
This needs to be done inside of the .htaccess file as the setup that the website currently has.
I know there is a way in which you can redirect 404 to /example however this is resulting in a 200.
Is their a way I can say in the htaccess file that if it is .js .css to look in /example?
Thanks in advance
EDIT:
For a little but more information, my current htaccess is like this
DirectoryIndex index.php index.html
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /example/index.php [NC,L]
It is for php because the php echos a file_get_contents of the index.html which is an Angular project.
So I need this htaccess to be the following logic
If the file is a .js or .css then rewrite the location to /example else rewrite the location to example/index.php.
The reason this is happening is because I am doing a format which has the ID as a second parameter and for some reason this is interfering with the way that the URL is structured for the js, css.
I imagine this line is what is breaking it...
RewriteRule ^(.*) /example/index.php [NC,L]
Converting my comments to answer. This appears to be problem due to relative links.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/example/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

css, images and vendor folders do not load when using htaccess rewrite

Below is the htaccess scripti am using:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^gecko/([^/]*)$ gecko.php?gecko=$1 [L]
Which changes http://localhost/geckology/gecko.php?gecko=Zilly (which loads css etc fine) to http://localhost/geckology/gecko/zilly which doesn't load the css etc fine as it looking in directories like this http://localhost/geckology/gecko/css/theme.css when it should be http://localhost/geckology/css/theme.css
The site is huge, so if possible i would like a htaccess way of fixing this, however i will change everything to absolute urls if it's not possible
You can fix the relative URI base by simply adding this to the header of your pages:
<base href="/" />
or if you have to use htaccess, which is really inefficient and will assume all of your css/scripts/etc are all in one place, while at the same time making the rest of the world think the same thing is actually 2 different URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gecko/(css|images|other)(/.*)$ /$1/$2 [L]
This blindly rewrites any URL that tries to access "css", "image" or "other" folders within the /gecko/ path.

htaccess rewrite url with extra slash

I have this code for the htaccess file, and soon I'll have pages where I'd like to create a url like the bottom one
RewriteEngine On
RewriteRule ^Index/?$ index.php [NC]
RewriteRule ^Gallery/?$ gallery.php [NC]
RewriteRule ^Showreel/?$ showreel.php [NC]
RewriteRule ^Music/?$ music.php [NC]
RewriteRule ^Gallery/Render/?$ contact.php [NC]
If I go to the contact page, it'll display a page, but it's entirely white and only contains the text. I'm guessing it's to do with the images not being linked up properly with the new url, but how would I actually go about fixing this without having to manually edit the locations of each image?
You can try adding this in your page's header:
<base href="/" />
or change all of your links to absolute URLs. This issue is most likely that your links are all relative, and when you try to request a URL like:
http://yourdomain.com/Gallery/Render
The relative URI base becomes /Gallery/ instead of / (which is what it is if you access /contact.php directly). And when the browser tries to resolve all the relative links on the page, it uses the wrong URI base.

htacces - need to fix broken links coming from other sites to mine

I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]

.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.

Resources