.htaccess sub-folders to read parent folder file while maintaining 'pretty' url - .htaccess

This will have been asked and will have an answer. I cannot remember what this feature is and attempts to once again learn it have come up dry.
I'm looking to place a .htaccess file within a folder where the file to read is located, anything beyond that will read that file.
Example:
www.url.com/articles/animals
www.url.com/articles/animals/safari
www.url.com/articles/animals/safari/lions...
Would display the file at
www.url.com/articles/file-to-read.php
So you can then grab the animals, safari, lions to display the necessary content*, maintaining the 'pretty' URL.
*To clarify, I'm not asking for the code to obtain the path parts, this is just an example usage of said parts.

I think you are talking about the pattern matching and rewriting to the actual file on server, something like below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^articles/(.*)$ articles/$1.php [L]
Or
RewriteRule ^articles/(.*)$ articles/filetoread.php [L]

Related

.htaccess - Open files without extension using .htaccess in subdomain

I downloaded a website, all files are coded in HTML and files do not have any extension.
My root domain is WordPress based.
I want to open all files as HTML using .htaccess - the files are in a subdomain.
I have tried this, think its for root domain only... I need help for subdomain.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
Your question is a bit vague as you've not given any examples of the URLs and file structure involved. However, if the HTML files do not have file extensions then it's quite probable that the Content-Type HTTP response header being sent from the server is either wrong or entirely absent (and reliant on the browser "content sniffing" - which is going to get mixed results).
We need to make some assumptions:
All the HTML files (that do not have file extensions) do not contain a dot anywhere in the file path.
The CSS and JS files do have appropriate file extensions ie. .css and .js, and are already returning the correct mime-type (Content-Type header).
If these too don't have file extensions, then they would need to be contained in specific directories so we can set the appropriate mime-type accordingly. We would also need to create exceptions with our rules in order to avoid conflicts.
So, in order to get the browser to interpret these files as HTML we need to make sure that we are sending the correct mime-type (ie. text/html) in the Content-Type response header. (I assume this is not the case currently.)
For example, using mod_rewrite in the .htaccess file in your subdomain (which I assume is separate from your main domain):
RewriteEngine On
RewriteRule ^([^.]+)?$ - [T=text/html]
However, as noted above, if your static resources (JS, CSS, images, etc.) are also devoid of file extensions then we'll need to add conditions to the above rule and create additional rules for the different file/mime types.
Aside:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
These directives that you posted in the question don't appear to have anything to do with your question, which has only added to the confusion in comments.

.htaccess configuration for accessing different pages

I currently use the .htaccess file to "shortcut" the URL to a user's profile:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /gd/profile.php?username=$1
Now I want to do the same for "tags". When pressing a tag, go directly to a "tag page". For that I am adding the following to the code above:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /gd/tags.php?tag_name=$1
The thing is that now the profile one doesn't work. How can I configure both so that neither one influences the other?
Thanks
You are just catching “everything” with your pattern – so how do you expect the server to decide whether /foo should be a user profile or a tag …?
Unless you want to feed everything to one script and use your database to look up whether that refers to a user profile or a tag, I suggest you use urls of the form /user/foo and /tag/foo to distinguish between the two types – then you can easily catch the beginning user/ resp. tag/ in your RewriteRules for the two cases.
Apart from being easier to technically realize, this also gives your users the ability to see what content to expect from the URL path only, and you will also have no problems if you have a user profile and a tag named “foo” (which otherwise would lead to a problem).

htaccess file properly detects missing file from root directory but does not detect missing subdirectory

I have an .htaccess file that I'm using to catch when a requested image doesn't exist so I can make one. The file properly detects a missing image when requested from the directory the .htaccess file is in. But, if I request an image from a subdirectory that technically does not exist, .htaccess does not send me to my image handler. Can someone help me match any subdirectory that does not exist?
Here is my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} .(jpg|jpeg|gif)$
RewriteRule ^(.*)$ missingimage.php?img=$1 [QSA,L]
That .htaccess file is placed in a folder called "uploads". So for example, my uploads folder also has oranges.jpg.
The following url will correctly pass the request to missingimage.php:
http://localhost/uploads/oranges_asdf.jpg
The following url will NOT correctly pass the request to missingimage.php, but instead returns a standard 404 from apache:
http://localhost/uploads/not_a_real_directory/oranges.jpg
How can I modify my htaccess to catch requests to directories that don't exist and still pass them to my image handler? Thank you.
REQUEST_FILENAME doesn't actually contain the full absolutely path if the request doesn't exist. My theory on this without actually digging into httpd core code would be that it's thinking we've come far enough to know that the request isn't actually there so let's stop looking.
i.e. if you request /uploads/fake/test.jpg it'll REQUEST_FILENAME contains /path/to/uploads/fake and won't actually continue to append /test.jpg on there, but does when you do /path/to/uploads/test.jpg because test.jpg is the termination of where it knows the request doesn't exist. Even if this isn't the reason why httpd stops the string there you have plenty of other variables that can help you.
This rewrite condition:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
is ridiculously popular right now for this trick of creating a dispatcher for MVC frameworks and does actually work, since /path/to/uplaods/fake is niether a file nor a directory and will send your request to your image dispatcher.
Here's the real fix for your extension matching condition... you need to get the REQUEST_URI because it will contain what the user actually requested (/uploads/fake/test.jpg) including the file extension and you can us it in your third RewriteCond to match the file extension. Since we're going to use that for the third rule, I cleaned up your first two to match.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} ^[A-Za-z0-9\/_-]+\/[A-Za-z0-9_-]+\.(jpg|jpeg|gif)$
RewriteRule ^(.*)$ missingimage.php?img=$1 [QSA,L]
I cleaned up your match on the file name as you were matching all sorts of bullshit with that leading period which is actually (when un-escaped) telling the preg match engine to match anything. So, backslash period will make preg match an actually period.
I also cleaned up the match on the first half to the request uri to include /alpha/num/directories_with/underscores-and-dashes/followed/by/alpha-num_filenames.jpg|jpeg|gif, feel free to remove that if you don't want it.

How to setup optional parameters in mod_rewrite

I am in a new project and I'm designing the URL structure,
the thing is I want URLs look like this:
/category-23/keyword/5/
Where the normal page is:
/search.php?q=keyword&cat=23&page=5
So my question is, cat and page fields, must be optional, I mean if I go to /keyword it should be
/search.php?q=keyword (page 1)
and if I go to
/category/keyword should be:
/search.php?q=keyword&cat=category&p=1
and also if I go to
/keyword/5/ it must be: /search.php?q=keyword&p=5
Now I have my .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ search.php?q=$2&cat=$1&page=$3 [L]
I cannot make it working and the CSS / image files don't load.
I'd thank a lot who could give me a solution.
You can do this with four rules, one for each case:
RewriteRule ^([^/]+)$ search.php?q=$1
RewriteRule ^([^/]+)/([0-9]+)$ search.php?q=$2&p=$1
RewriteRule ^([^/]+)/([^/]+)$ search.php?q=$2&cat=$1&p=1
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)$ search.php?q=$2&cat=$1&p=$3
And with this rule in front of the other rules, any request that can be mapped onto existing files will be passed through:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ - [L]
Now your last issue, that externally linked resources cannot be found, is due to that you’re probably using relative URL paths like css/style.css or ./css/style.css. These relative references are resolved from the base URL path that is the URL path of the URL of the document the references are used in. So in case /category/keyword is requested, a relative reference like css/style.css is resolved to /category/keyword/css/style.css and not /css/style.css. Using the absolute URL path /css/style.css makes it independent from the actual base URL path
While i know this was answered succinctly by #Gumbo a few months back, I ran into a similar issue recently... and didn't want to include full/absolute paths in my app, to keep it dynamic and not having a bunch APP_PATH (php) vars all over the place... so I just added a base[href] html tag with the
like so...
<base href="http://<?php echo $_SERVER[HTTP_HOST];?><?php echo APP_PATH;?>"/>
Hoping that helps others... and this is not trying to discount #Gumbo's reply in the least... they're right-e-o on :).
Shouldn't it be
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]
in #Gumbo's answer above? Works with me like that. If it is a file (like CSS), let it pass through.

Rewrite a request whether or not the requested file exists

I'm trying to convert all requests in the format:
/portfolio/picturename.htm
('portfolio' is constant)
to this:
/?picturename
So (thanks to users here) I have this solution which works for files that don't exist:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ /?$1 [R,NC,L]
But: How do I also have this apply to files that exist? I'd like the redirect to happen under all circumstances.
This should work for all files, whether they exist or not. RewriteRule doesn't check for file existence, RewriteCond does. Are you posting the full content of your .htaccess file, or only the part you think is important?
There's nothing about that RewriteRule that would make it only apply to files that don't exist. It would need this before it for that condition:
RewriteCond %{REQUEST_FILENAME} !-f

Resources