.htaccess to remove the need for adding .html in the address bar versus creating new folders for all pages - .htaccess

I'm sure that with any effort I could figure out how to make this work inside of a .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
But, would that be more or less efficient than just creating a folder with new index.html in each folder?
For me, the real end result that I'm trying to achieve is to not require somebody to type:
skypodstudios.com/solar.html
I would rather let it be good enough to just type:
skypodstudios.com/solar
Both seem to accomplish it, I'm just wondering which is more efficient or if either are frowned upon?

Using folders instead of files is a bad practice. You don't need a folder for each html file you create and it will only invite clutter. Using the .htaccess solution you specified is also not optimal since it will force you to put all your html files in one folder (and will not be useful if you use other file types).
I suggest you use one of the following instead:
For a small project with few files, include HTML files depending on the URI segment. You do that by telling your .htaccess to pass all url queries to your index file (usually index.php) and then that file breaks down the URL segments and retrieve the correct file from the correct folder.
For a big project with hundreds of files, ask yourself if you are using the same template (file structure) over and over again. If your project is a blog or a store chances are the answer is yes. If it is then instead of including files you'd better store your data in a database and then retrieve the current page's data via your index.php file using a database query.

Related

Copy Website to new Directory

I have a rather large website that I need to move to a different directory. Right now, the website has a normal structure.
www.technology.com
The company wants the entire website moved so the new main URL will be:
www.technology.com/structure
So, the current page structure which is:
www.technology.com
www.technology.com/about
www.technology.com/services
www.technolgy.com/products
needs to become:
www.technology.com/structure
www.technology.com/structure/about
www.technology.com/structure/services
www.technolgy.com/structure/products
This is an older website that isn't inside of a CMS. Would the easiest way to do this be to actually just create a directory in the root called structure and copy everything into it?
What would I do as far as catching any people that might have links bookmarked? So, if someone were to come to www.technology.com, I would want them to automatically be redirected to > www.technology.com/structure and vice-versa with everything else. I'm assuming this could be accomplished with the .htaccess file.
Any help would be appreciated.
Yes this is right just move your complete website to www.technology.com/structure and put this in to your .htaccess file in www.technology.com/
RewriteCond %{REQUEST_URI} !^/?structure(/.*)?$ [NC]
RewriteRule ^/?(.*)$ /structure/$1 [R=301,L]
One remark: The whole thing would only work if on your apache server mod_rewrite is enabled and you are allowed to use htaccess files (with mod_rewrite), this is not always the case by default.

htaccess removing index.php and search queries from URL

I have a site that, for a certain php function to work, needs the url to be:
/topic/index.php?height=###
I would like the URL to read
/topic/
What can I put in the .htaccess file to achieve this? Can I put one htaccess file in the root, or would I need to put one in each /topic/ directory?
I think the following might work for your issue:
RewriteRule ^([^/]*)/?$ $1/index.php?height=###
Of course, that's assuming a static number. If you need a dynamic number or one provided by the client, you're going to need something like:
RewriteRule ^([^/]*)/(\d+)/?$ $1/index.php?height=$2

Htaccess Image redirect to specific resize directory

Hopefully someone here can point me in the right direction as htaccess is driving me crazy at the moment.
What i am trying to achieve is an automatic redirect for certain images
Currently I use jquery to replace the image src.
The reason for this is the new resized images are in a different directory.
The problem with this method is every time we refresh we have to wait for the dom to fully load.
And I see this is possible with htaccess.
Redirect /my-domain.com/images/image1.png /my-domain.com/images/resized/image1.png
Currently this works, but for over 100 images I really need to find a dynamic solution for this
I tried the following which obviously failed.
RewriteRule ^/my-domain.com/images/(.*) /my-domain.com/images/resized/(.*) [R=301,L]
the resized directory has several directory's so the rule needs to apply to all child directories.
Although it's not a big problem to list all the directories as long as I don't list all images.
hopefully I am missing something simple here, also I wanted to make sure the redirect will not effect SEO?
maybe there is an alternative solution with htaccess?
This is a bit of a messy way to handle images - across multiple folders - but, if that's how you want to manage it, fair enough.
From the above, I understand that:
There are some images within the /images/resized/ folder
There are also some images within subfolders of the same
You want to be able to call a URL within the /images/ folder and have it transcribed to the /images/resize/ folder (with the same end)
In the webroot's .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/images/
RewriteCond %{REQUEST_URI} !^/images/resized/
RewriteRule ^images/(.+)$ /images/resized/$1
Tested OK with this htaccess tester.
"I wanted to make sure the redirect will not effect SEO?"
Filenames are not as important for SEO as alt tags and titles. There should be no change to the SEO stance of your site as a result of this change.

Fastest way to redirect missing image files

I have an on-the-fly thumbnailing system and am trying to find the best way to make sure it's as fast as possible when serving up images. Here is the current flow:
User requests thumbnail thumbnails/this-is-the-image-name.gif?w=200&h=100&c=true
htaccess file uses modrewrite to send requests from this folder to a PHP file
PHP file checks file_exists() for the requested image based on the query string values
If it does:
header('content-type: image/jpeg');
echo file_get_contents($file_check_path);
die();
If it doesn't it creates the thumbnail and returns it.
My question is whether there is a way to optimize this into being faster? Ideally my htaccess file would do a file_exists and only send you to the PHP file when it doesn't... but since I am using query strings there is no way to build a dynamic URL to check. Is it worth switching from query strings to an actual file request and then doing the existence check in htaccess? Will that be any faster? I prefer the query string syntax, but currently all requests go to the PHP file which returns images whether they exist or not.
Thank you for any input in advance!
You should be able to do this in theory. The RewriteCond command has a flag -f which can be used to check for the existence of a file. You should be able to have a rule like this:
# If the file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
# off to PHP we go
RewriteRule (.*) your-code.php [L,QSA]
The twist here is that I imagine you're naming files according to the parameters that come in -- so the example above might be thumbnails/this-is-the-image-name-200-100.gif. If that is the case, you'll need to generate a filename to test on the fly, and check for that instead of the REQUEST_FILENAME -- the details of this are really specific to your setup. If you can, I would recommend some sort of system that doesn't involve too much effort. For example, you could store your thumbnails to the filesystem in a directory structure like /width/height/filename, which would be easier to check for in a rewrite rule than, modified-filename-width-height.gif.
If you haven't checked it out, Apache's mod_rewrite guide has a bunch of decent examples.
UPDATE: so, you'll actually need to check for the dynamic filename from the looks of it. I think that the easiest way to do something like this will be to stick the filename you generate into an environment variable, like this (I've borrowed from your other question to flesh this out):
# generate potential thumbnail filename
RewriteCond %{SCRIPT_FILENAME}%{QUERY_STRING} /([a-zA-Z0-9-]+).(jpg|gif|png)w=([0-9]+)&h=([0-9]+)(&c=(true|false))
# store it in a variable
RewriteRule .* - [E=thumbnail:%1-%2-%3-%4-%6.jpg]
# check to see if it exists
RewriteCond %{DOCUMENT_ROOT}/path/%{ENV:thumbnail} !-f
# off to PHP we go
RewriteRule (.*) thumbnail.php?file_name=%1&type=%2&w=%3&h=%4&c=%6 [L,QSA]
This is completely untested, and subject to not working for sure. I would recommend a couple other things:
Also, one huge recommendation I have for you is that if possible, turn on logging and set RewriteLogLevel to a high level. The log for rewrite rules can be pretty convoluted, but definitely gives you an idea of what is going on. You need server access to do this -- you can't put the logging config in an .htaccess file if I recall.

Issues when creating pretty URL that uses actual site urls

I want to create functionality similar to the site downforeveryoneorjustme.com. They use a pretty URL to take in the URL of any given site. I sure they use htaccess to do this, however the method i'm using is encountering problems.
This is my .htaccess file that I'm using to send the site URL to a file.php:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)?$ /file.php?var=$1
However when I type in something like
mysite.com/http://google.com the variable it sends the file is http:/google.com (missing a slash). I can't figure out why this is occurring.
Also, when I type in something like mysite.com/existingfolder, where existingfolder is a folder on my site, it always works incorrectly. The variable it passes to the file is missing.html instead of existingfolder. In this case, the file doesn't display images. The image can't be found, and i'm assuming its because it's searching for the image in an incorrect folder on the site. That it might think it's in existingfolder and not in the normal folder it should be in.
Does anyone know why I'm getting these problems? I'm knew to htaccess, and I'm assuming it has something to do with that.
Thanks for any help.
I sure they use htaccess to do this
I'm not. I'm not even sure they're using Apache.
mod_rewrite is not always the answer to all URL-processing problems. It's certainly prone to some of the quirks of path-based URL handling, including the removal of double-slashes.
I suggest reading the Apache-specific REQUEST_URI variable from your script, rather than relying on rewrites to get a parameter. This will give you the path requested by the browser without any processing.

Resources