I have a rewrite condition for specific file types like so:
RewriteCond %{REQUEST_URI} .*(\/|.htaccess|.htpasswd|.ini|.log)$
This works fine for files with those file extensions however I can't figure out how to make it also match files with no extension.
Eg. cats.com/regularfile.ini and cats.com/regularfile should both trigger the condition.
Any ideas?
I am fine if the no extension condition needs to be separate but it should trigger whether the file actually exists or not as my current rule does.
I can't figure out how to make it also match files with no extension.
You may use it like this:
RewriteCond %{REQUEST_URI} ^/(?:[^.]+|.*(/|\.(?:htaccess|htpasswd|ini|log)))$
[^.]+ match 1 or more of any character that doesn't have dot.
Related
I always have a single file per folder named test.* (* = jpg, png, video file etc) which is stored in different sizes as following
https://www.example.com/images/1500/01/100/test.jpg
https://www.example.com/images/1500/01/500/test.jpg
https://www.example.com/images/1500/01/900/test.jpg
https://www.example.com/images/1500/01/1800/test.jpg
I would like to create a rule in htaccess which will allow me to load the different files using a link.
As an example for the first image I would like to be able to load the file using the following links:
https://www.example.com/images/1500/01/100
https://www.example.com/images/1500/01/100/
https://www.example.com/images/1500/01/100/whatever
https://www.example.com/images/1500/01/100/somethingelse
The logic would be to always load the file which will always be named test however without calling it test in the link and in addition be allowed to write anything behind the trailing slash.
Is it possible using plain htaccess. I was trying with the following however it does not give the result I am looking for:
RewriteEngine On
RewriteRule ^images/(.*)/([^/]+) images/$1/$2/test.jpg
If you have a hierarchy of 3 folders deep, and they are all numeric you can do something like this:
RewriteEngine On
RewriteRule ^images/([0-9]+)/([0-9]+)/([0-9]+).+?$ images/$1/$2/$3/test.jpg [L]
This will match
https://www.example.com/images/1500/01/100
https://www.example.com/images/1500/01/100/
https://www.example.com/images/1500/01/100/whatever
https://www.example.com/images/1500/01/100/somethingelse
If they are not always numeric:
RewriteEngine On
RewriteRule ^images/([^/]+)/([^/]+)/([^/]+).+?$ images/$1/$2/$3/test.jpg [L]
I imagine this has to be a common scenario but I'm struggling to describe it sufficiently well or to find a working answer!
Essentially I want to make hundreds of URLS that include unique reference codes but that are easy to type in the form example.com/aabbcc, which will be intercepted and all delivered to a PHP script for validating that code, located somewhere like example.com/script.php.
I need the subdirectory part of the URL (aabbcc, in this example) to become a GET parameter for that script, so a URL like the one above would be sent to example.com/script.php?id=aabbcc, while hiding this more complicated URL from the user.
I can see from other .htaccess examples that this must be possible, but I can't find one doing this.
Is there a .htaccess solution for it? Is there something else even more basic? Your help is appreciated in steering me.
If your "unique reference codes" consist of 6 lowercase letters, as in your example then you can do something like the following in your root .htaccess file using mod_rewrite:
RewriteEngine
# Internally rewrite "/abcdef" to "script.php?id=abcdef"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[a-z]{6}$ script.php?id=$0 [L]
If you don't need direct access to any subdirectories off the root that also happen to match a "unique reference code" then you can remove the preceding condition (RewriteCond directive). With the condition in place then you naturally can't access any "unique access codes" that happen to also match the name of a subdirectory.
$0 is a backreference to the entire URL-path that the RewriteRule pattern (first argument) matches against.
Reference
Apache mod-rewrite Documentation - Contents
Apache mod_rewrite Introduction
Apache mod_rewrite Reference
RewriteRule Directive
RewriteCond Directive
I have a file in a folder like this folder1/folder2/folder3/filename.htm. I also have another file like this folder1/folder2/folder3-filename.htm. When i browse the url mysite.com/folder1/folder2/folder3/filename.htm, i want the folder3-filename.htm will be served (displayed)
I know some basics about file name rewrite rules, but i have not seen any mixed-up combination of folder name and file name in that way.
Could anyone help me how to do it?
Using the expression [^/]+ to match all characters up to but not including the the next / and then .+ to match everything after the last /, the following will work:
RewriteEngine On
RewriteRule ^folder1/folder2/([^/]+)/(.+)$ folder1/folder2/$1-$2 [L]
I want to create a bunch of files without an extension showing at the end. The easiest way to do that was to do this:
/usa/index.php
/usa/alaska/index.php
/usa/alabama/index.php
/usa/california/index.php
What I want to do is this
/usa/alaska.php
/usa/alabama.php
/usa/california.php
and have it show up as:
/usa/alaska
/usa/alabama
/usa/california
However, I have one more level I want to add to this, the cities
/usa/alaska/adak.php
/usa/alaska/anchorage.php
/usa/california/los-angles.php
I don't want the ".php" showing up, but then each state exists as both a file and a directory. What I want is an htaccess rule that serves up the file version of the file, not the directory which is the default. I also want to strip the .php off of the end of the files so the final result looks like
/usa
/usa/alaska (alaska.php)
/usa/alaska/adak (adak.php)
I know I can get close to this by creating all the directories and using index.php for each directory, but then I will have thousands of directories each with one file in it and updating is a pain in the butt. I would much rather have one directory with 1000 files in it, than 1000 directories with 1 file in it.
Please, can someone point me in the right direction and know that I am doing this for all 50 states.
Jim
I would also suggest using a single php (e.g. index.php) file and redirecting all urls starting with usa to it, instead of separating them in different directories and files. The you'd need a couple of rewrite rules like the following
RewriteEngine On
RewriteRule ^usa/([^/.]+)$ index.php?state=$1 [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ index.php?state=$1&city=$2 [L]
So then in your index.php you'd only need to check the $_GET parameters.
Update:
If you don't feel comfortable enough to use a database and pull the needed data from there you could always use the parameters to dynamically include/require the needed files. Something like this
<?php
$source = ''; //or the 'ROOT' directory
if(isset($_GET['state'])) $source .= $_GET['state'].'/';
if(isset($_GET['city'])) $source .= $_GET['city'].'.php';
include($source); // here $source would be something like 'alaska/adak.php'
// and is assumed that the dir 'alaska' is on the same
// level as 'index.php'
?>
But to answer your original question nevertheless you could use the following .htaccess
RewriteEngine On
RewriteRule ^usa/([^/.]+)$ usa/$1.php [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ usa/$1/$2.php [L]
what about creating just one single file:
/usa/index.php
With
$_SERVER["REQUEST_URI"]
you can read the current URI.
Well, now if a user enters "http://domain.foo/usa/alaska" for example, he will get an 404 error of course.
But to call your index.php instead, you could write this line to the .htaccess:
ErrorDocument 404 /usa/index.php
Now the index.php receives everything what is written to the URI and you can match the result and include files or handle errors.
But maybe there is a better solution with .htaccess only, don't know. :)
I've got a directory called fb and a script inside called like.php. I'd like to have the get-id passed to the like-file using mod_rewrite.
mypage.com/fb/like.php?id=5 would be mypage.com/fb/like/5
My (not working) htaccess looks like this:
RewriteEngine on
RewriteRule /fb/like/([0-9]+) /fb/like.php?id=$1
Does anyone see what's wrong here?
Try removing the slash at the beginning of your match and replace patterns like so:
RewriteRule fb/like/([0-9]+) fb/like.php?id=$1
mypage.com/ is the domain name, so the string that gets matched is fb/like/5
Also consider using the carat at the start of your match string so that it will match fb but not fffb:
RewriteRule ^fb/like/([0-9]+) fb/like.php?id=$1
Here's a short guide to mod_rewrite I've found helpful.
Edit for your follow-up question:
To match mypage.com/something/fb/like/5, you can do this:
RewriteRule ^([^/]+)/fb/like/([0-9]+) $1/fb/like.php?id=$2
This saves the first directory as $1. [^/]+ means match one or more characters that are not a slash. Put this .htaccess file in the root directory of your domain.
Alternatively, you can use the second-to-last rule and put that .htaccess file in the "something" subdirectory. Hope that makes sense.
Or you can write a rule to match simply like.php/([0-9]+) so that it'll work no matter what the directory path looks like. You can go even more generic and make this apply to any PHP file, not just like.php. It really depends on how you want your site to work.