Silly 404 page usage? - mod-rewrite

Alright, so the only way I know of changing links to not show the file extensions;
this yourwebsite.com/customlink.php to yourwebsite.com/customlink
is either creating a folder called customlink and shoving an index file in there. Or creating a 404 page which snoops around the URL and grabs whatever string is behind the last / and does whatever to show the proper content.
My question is if this way of solving the problem is straight out idiotic, or not? I'm doing this because in my mind it saves space, let me explain: Every page that needs a customlink are the same with some bits and content taken from other places, so instead of creating X amount of folders and index files that includes a main file, I'll just have one 404 file that can handle it.
I apologize in advance if this is really stupid

As you are not mentioning what server your pages are running on (Apache, IIS, ...?), I'll just assume Apache.
Put an .htaccess file into the root of your site with the following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It will internally rewrite everything without an extension to the corresponding php file, provided that:
the request is not a valid directory
a file with a php extension is in fact present

A more common approach is to route all HTTP requests to a single index.php using mod_rewrite in a .htaccess.
Then, based on the requested resource, the index.php outputs the appropriate file, whether it is generated from a database or nested in some other folder.
It's probably not a good idea to use a 404 page (an error page) for anything other than "File Not Found" instances.

Related

mod_rewrite to extensionless file results in 404

On my server I have an image cache where all the image files are saved with the hash of their original location as their file name, without extension. I want to serve the images via mod_rewrite, with their original filename at the end of the URL.
For example, the physical location of an image file on the server is
/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273
I want to serve this image with the url
https://example.com/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273/OriginalFilename.jpg
So, I added this to my .htaccess file:
RewriteEngine On
RewriteRule ^_img_cache\/([a-z0-9])\/([a-z0-9])\/([a-z0-9]+)\/.+$ _img_cache/$1/$2/$3 [L]
It should just strip off the original filename part and serve me the file.
I've tried this, but it gave me a 404 in return.
Unable to find my mistake, I made some slight modifications, added an extension to my test file, and changed the RewriteRule accordingly:
/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273.jpg
RewriteEngine On
RewriteRule ^_img_cache\/([a-z0-9])\/([a-z0-9])\/([a-z0-9]+)\/.+$ _img_cache/$1/$2/$3.jpg [L]
If I request the same URL, I get my image served without problems. But it's not really the way that I wanted. I want it to work with the files as-is; extensionless. And that seems to be the only variable causing it to fail.
Why is Apache claiming the file without an extension, after rewrite, does not exist, while I've verified multiple times that it does. Is it a bug or a feature? Or am I missing something else? :)
Try this adjustment:
RewriteRule ^(_img_cache/([[:xdigit:]])/([[:xdigit:]])/\2\3[[:xdigit:]]{38})/.+ $1 [END,DPI]
Refer to the flags documentation, which warns that L can cause looping, which is rarely what people want. It should usually be combined it with DPI, and END is preferable if you don't need to check the rewritten path against any other rules.
Here's the explanation for the simplified regex.

Rewrite numbers to default page

I am trying to rewrite a condition that can take any page that has numbers.html, lets say the like the following:
12345.html
33443.html
234545656.html
9797423.html
and just redirect it to index.php
RewriteRule ^([0-9]+)$ index.php [L]
I tried the following but it doesn't work, any help is very much appreciated.
Two immediate issues with your code:
you have to switch on the rewriting engine prior to using it and
if you want to match file names ending with .html, then you have to code that.
Have a try with this:
RewriteEngine on
RewriteRule ^([0-9]+)\.html$ index.php [L]
Then a few more hints:
the code you tried will only work in .htaccess style files. But the usage of such files has to be enabled first in your http server configuration.
a .htaccess style file has to be placed at the correct location, in this example within the folder actually holding the index.php file.
the rewriting module has to be installed and enabled, without those commands will not be available.
And a general remark:
Those .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server, often for nothing. You should always prefer to place such commands inside the real host configuration of your http server. .htaccess style files are only offered as a last option for situations where you have no access to that configuration, for example when using a really cheap shared hosting provider.

Laravel infinite loop

I'm building an app using Laravel v.4.2. In the app/public directory, I have images folder which contains static images. Then, I created an ImagesController for users to manage assets. However, when I entered the URL: myapp.dev/images into browser, I got an infinite redirect loop error. I double checked all my routes and noticed there're no ones related to images path. Even when I commented out all routes entries, the error still exists.
I found a work around to this by rename the controller, however this is not going to be an ideal solution.
What can I do to completely deal with this error?
You will have to rename either your images directory, or your images route. What's happening is expected behavior of your server: when it hits the public directory (which is where all Laravel requests begin), it's finding that the images folder exists, so your URL is not redirected to index.php and your application is not launched. So it never even gets to your routes.
The cause of the loop itself depends on the contents of your .htaccess file, but it is probably happening because when you request myapp.dev/images, your server recognizes that images is a directory and immediately returns a 301 redirect to myapp.dev/images/ (with the trailing /). Then, your .htaccess file jumps in and tries to convert it back to myapp.dev/images, without the trailing slash. This happens in the line RewriteRule ^(.*)/$ /$1 [L,R=301].
Normally, when navigating to a folder in app/public, you should get a 403 Forbidden error. The line that does this is Options -Indexes, which disables the ability to display a list (or index) of directories in the browser. Usually, you have indexes disabled server-wide, in your server's httpd.conf file. You might want to check that that's the caseā€”or at the very least, add Options -Indexes to your .htaccess file in app/public.
Also, make sure that your .htaccess file in app/public contains both of these lines:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

.htaccess MOD_REWRITE specific cases

I am really struggling with .htaccess since I can't figure out how the syntax actually works and what the commands are. Let me describe what I need to achieve and maybe someone could guide me to the right answer:
I have a website which works with JQuery Ajax. So there is one single index.php file in the root folder. Any request of a sub-page via internal link(mysite.com/contact.php) is going through Ajax and the content information gets loaded from a folder called "/pages/" to the index.php.
But if a user enters the url itself (mysite.com/content.php) Everything breaks since the directory actually doesn't exist. Remember the content files are inside /pages/. And a direct access to the raw content files would not display the site, but only the information in raw form.
To solve this I started using .htaccess to pass not existing directory request to php, which passes it to Ajax. But here I got stuck. I am really bad with .htacces. This is what I got from the internet:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This works for the most part, except if the request goes through a subfolder (mysite.com/download/version1)
So here is what I need .htaccess to do:
Don't care if it is a folder or a file request, pass it to php.
(optional) Do not allow direct access to /pages/ folder, but do allow if request comes via Ajax
Do not mess up request calls for resource files. (This is important. When I fiddled around, I got many errors since .htaccess was also interfearing in resource file (.css/.js/.png) calls)
Feel free to take a look at the website trough: GitHub
The php part is inside index.php. The Ajax is done in PageHandler.js

mod rewrite all url's except includes (js/css/img/some php)

What I want is a Wordpress type of URL rewrite.
What I have now is:
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
This almost works rewriting everything except existing files (css,js etc files are not rewritten as expected).
The problems I have with this are:
.css, .js, Image and PHP files are also accessible by entering their file name. eg.: domain.com/style.css will be accessible by anyone.
I want some existing files(only php right now) to be redirected anyway. eg.: domain.com/movies.php should redirect to rewrite.php(include 404.php) and domain.com/movies or domain.com/movies/ would include movies.php page.
Ideally I would also be able to change user entered URL from domain.com/movies to domain.com/movies/ for consistency more than anything else.
I want to keep .htaccess to a bare minimum. Does wordpress rewrite everything including .css files?
What I want:
Some php files to redirect others not. eg: includes/func.inc.php
should not be accessible to the users, while movies.php should be
accessible but ONLY from this url domain.com/movies/
(not essential)
Change url to canonical url eg: domain.com/movies to
domain.com/movies/ (some resources on how to achieve this would be
nice). Note: domain.com/movies url should still work but appear with
a slash at the end either via rewrite or maybe by just adding a
slash with javasript (faster?)
Make .css/.js files inaccessible by the user. eg: domain.com/style.css should redirect the user to a 404 page

Resources