How to convert ULR not existent subfolder to GET variable in .htaccess? - .htaccess

Need to convert subfolder named exact length of chars to GET variable. If posible, code that will work and on any other than 127.0.0.1 address.
Perfect will be that logic - if in request is some subfolder named with three chars(letters or numbers), and this folder is before last slash(/) - just convert this subfolder to GET variable, maintain any other GET variables if was there.
Need to convert path from this:
http://127.0.0.1/truck/c4/sD2/
To this:
http://127.0.0.1/truck/c4/?r=sD2
Or this:
http://127.0.0.1/truck/c4/index.php?r=sD2
Very important, "sD2" - is just random code for internal purposes. It generated randomly, so it not be like that always. But it always has three chars and slash(/), so somehow need to identify htose three chars and rewrite URL to transfer it with GET variable.
Also it should keep any other GET variables in current URL if has so.
Also need to .htaccess file to be on /truck/c4/ folder, and with possibiliy to move this root folrer to another domain.
For now i have this .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} /?([/]..[^/])/$
RewriteRule /?(...)/$ http://127.0.0.1/truck/c4/?r=$1 [R=301,L,QSA]
</IfModule>
It works, but i very need to untied code from specific address to make it more universal, if possible.

Related

Rewrite rule htaccess to ignore everything after last slash

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]

URL redirect to use subdirectory as GET parameter

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

htaccess Redirect Subfolder only (to lower case)

I've been searching and there are so many different examples here, but can't seem to find exactly what I need. Shared Environ...no access to httpd.conf
www.mysite.com/SuBfolder1/FiLe1.php (camel case)
www.mysite.com/SUBFOLDER2/FILE2.php (all caps)
www.mysite.com/subfoldeR3/filE3.php (one cap)
... and so on with many different subfolders...
In any of these instances...I'd want to redirect to the lowercase version of 'subfolderx'. But not affect anything below those folders. Some files under these folders may have caps too so I don't want the entire URL converted to lowercase.
Desired outcome:
www.mysite.com/subfolder1/FiLe1.php
www.mysite.com/subfolder2/FILE2.php
www.mysite.com/subfolder3/filE3.php
and so on for any subfolder
I think I could do something like this, but trying to do dynamic.
RewriteCond %{REQUEST_URI} ^/SuBfolder1
RewriteRule (.*) /subfolder1/ [R=301,L]

.htaccess not working as expected in sub-folder

I have this rewrite rule placed in /dashboard/.htaccess [dashboard is actually a folder]:
RewriteRule ^([^/]*)$ index.php?mode=$1 [L]
My structure is index.php?mode=support, even though, $_SERVER['QUERY_STRING'] outputs this:
mode=index.php
Example: site.com/dashboard/index.php?mode=support should be site.com/dashboard/support
So , how can I make it parse the param value, and not the file itself.
Managed to solve it while doing more research on regular expressions.
RewriteRule ^([a-z]+)$ index.php?mode=$1 [L,QSA]
Thi solved my problem, preferred plus instead asterisk because it tells the engine to repeat it zero or more times. (when i'm on index.php , query string is empty as needed)
Your rule is matching anything that starts with not a slash and doesnt contain a slash anywhere when your actual path is
/dashboard/support
to get the folder you actually want you need a base on there like this
RewriteBase /dashboard/
If that is placed above the rule, Then your redirect should be ok

htaccess - creating directories and files of the same name

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

Resources