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

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

Related

Silly 404 page usage?

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.

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 proxy URL missing images/css

I have the following mod_rewrite using Proxy flag to redirect from one URL folder to another site subdomain as follow:
The .htaccess file placed inside http://www.domain.com/test/ folder:
RewriteEngine on
RewriteRule ^($|/.*) http://subsite.site.com/$1 [L,P]
The problem, images, CSS and links are not showing up properly. Links appear to be pointing back to: http:// www. domin .com/linkname.html
I've tried doing RewriteBase /test/ and / with no luck, and couldn't figure out any other way to do it.
What am I missing in above code to make it work with relative paths at destination URL?
Oh, you want to change internal content?
Mod_rewrite only changes headers, not content and you would definitely need something else like mod_proxy_html. However, rewriting content just to change urls can normally be completely avoided (assuming you have control of your content) by making all paths legitimately relative. In such cases all paths in content should be like: linkname.html or some_path_from_here/linkname.html instead of /linkname.html or some_path_from_here/linkname.html

How to avoid ruining TinyMCE popups with url rewriting?

I have a website which uses TinyMCE textboxes for editing. It works fine except for the toolbar options which open a popup window as it seems to be caught by my url rewriting in the .htaccess file. Each of them show my 404-page. The url rewriting is set up so that all urls (except ajax calls) are sent to an index.php file. It seems obvious that the popup windows are caught by this as well but I have no idea what url signatrues to look for in the .htaccess file, so do anyone know what kind of pattern I can match against?
As #andyk pointed out, you can use a .htaccess in your TinyMCE folder to override the global settings that are doing the URL rewriting. I've done this in the past myself to resolve the same issue.
If that's not attractive to you for whatever reason, the other option is to exclude the tinyMCE folder like so:
# if URL starts with /TinyMCE, stop processing here
RewriteCond %{REQUEST_URI} ^/TinyMCE [NC]
RewriteRule .* - [L]
This sets a rewrite condition to check for the directory TinyMCE is in, and if found, the rule .* - means for all URLS, do nothing and the [L] means stop processing here.
try putting an exception in your htaccess for the tinyMCE folder maybe ?

Resources