.htaccess Remove filename from url - apache

I'm trying to remove: "site.php" from my url:s, which look like
"example.com/custom-foldername/site.php", so the url:s would look like "example.com/custom-foldername"
Currently it's not working at all. Basically my site just doesn't change the url. I know that the .htaccess file is being read, because if i write junk code in it, it gives error 500.
Here is my current .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options -Indexes
RewriteRule ^site\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site.php [L]
</IfModule>

The code you posted is a front-controller pattern - it rewrites every request for non-existent files to /site.php in the document root (not /custom-foldername/site.php).
And nor will a RewriteRule pattern like ^site\.php$ match a request like /custom-foldername if the .htaccess file is in the document root.
I'm trying to remove site.php from my URLs...
From all your URLs? Is site.php present in all your URLs? So, site.php is your front-controller?
If site.php really is your front-controller then try something like the following instead:
Options -Indexes
RewriteEngine On
RewriteBase /custom-foldername
RewriteRule site\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ site.php [L]
This rewrites everything that doesn't map to a physical file or directory to /custom-foldername/site.php.

UPDATE: I solved this my myself while ago, but forgot to update
So, i found the solution and here's my current .htaccess file, if someone is ever having the same problem
RewriteEngine On
RewriteBase /
Options -Indexes
DirectoryIndex site.php site.html
When user "asks" for folder directory (example.com/cats) apache looks for "site.php or site.html" files inside it. (example.com/cats/site.php) If it finds either on of them it displays it to the user as (example.com/cats). If either file isn't found it gives normal "404 not found error"

Related

.html removal from URL using .htaccess rewrite rules

I have a static website - a bunch of static html pages. I am trying to remove .html part from the URL of my webpages. I have used an .htaccess file with the following code to do that:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
However, I am getting a 404 error. For example:
The requested URL /home/username/public_html/contact.html was not found on this server.
Ideally, it should redirect to /~username/contact.html.
Addition information
When I used "Options -MultiViews" line above the code it is giving the following error:
500 Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (Red Hat) server is used here.
Why I am facing this problem? Is root is automatically getting changed from public_html/ folder?
EDIT:
Directory Structure:
Username
.gnome2 (there are empty folders inside it)
.mozilla (there are empty folders inside it)
public_html (I have put css, fonts, js, etc folders and .htaccess, contact.html, index.html, etc files here.)
.bash_logout
.bash_profile
.bashrc
Username folder is under universitynameuniverse folder (whose other folders I cannot see).
Try this code in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page.
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [QSA,L]
</IfModule>
## Results
# ~user/contact => ~user/contact.html (only if file exists)
It only works if the ~user/ directory actually exists on the filesystem.
If you need an external redirect instead, append an R flag to the RewriteRule directive.
The problem with your rewrite rule is that it's appending a .html suffix to the whole %{REQUEST_URI} variable which will probably result in a 404.
Update
Re-reading your question I noted that you want to map the ~user part to somewhere out of the apache webroot. In this case, try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/~(.+)/(.+)$
RewriteCond /home/%1/public_html/%2.html -f
RewriteRule . /home/%1/public_html/%2.html [QSA,L]
</IfModule>
## Results
# ~user/contact => /home/user/public_html/contact.html (only if file exists)

.htaccess, Laravel & Ikonboard

I have to admit, creating mod-rewrite rules still confuses me! So, I'm after some help please...
I've taken on a site built in Laravel, but now need to add an existing forum into the domain. The forum is Ikonboard, which on the live site lives in the cgi_bin folder. When I copy this to the new site, I can't get access because the htaccess is rewriting ALL URL's to the public folder (where Laravel want's it).
So, how can I make any requests to the cgi_bin folder work as well as keeping the rewrite to public for Laravel?
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Rewrite to 'public' folder
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
</IfModule>
Thanks
Simple:
RewriteCond {%REQUEST_URI} !^/cgi_bin
Add this above your other REQUEST_URI rule. If cgi_bin is in the URI, then it will stop rewriting.
Edit - Based on your response with regards to the directory structure, it seems to be wrong. Your www directory is your public directory. So, your structure should look like this:
/www/ (your public folder)
bundles/ (etc...)
cgi_bin/
.htaccess
index.php
/laravel/
(etc...)
Change your directory structure to look like that, and make sure that your .htaccess file has the following in it:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you call a CGI document, it should pass through because %{REQUEST_FILENAME} !-f is set above. All assets should pass through as well. You do not need to specify whether or not the cgi_bin folder is being requested. If it does not work, then add the line as I had originally in this answer (except, you would put the rule just under RewriteEngine On.

url rewriting - rewriting image path to internal folder

I want to add a rule to redirect jpg files to a directory beyond the document root. I already use the following which rewrites anything without a file extension.
Options Indexes FollowSymLinks Includes ExecCGI
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+$ /index.php/$1 [L]
I want to achieve
http://www.mysite.com/i/image_type/12345.jpg
=>
/internal_path/image_type/12345.jpg
EDIT In this case I ended up using a symbolic link. I am not sure if it was the best solution but it served my requirements.
For your image path, use this rule (put it at the end of your .htaccess) :
RewriteRule ^i/(.*)$ /internal_path/$1 [L]
Simply check this:
RewriteRule ^i/([a-z0-9-_]+)/([a-z0-9-_]+)\.([a-z0-9-_]+)$ /internal_path/$1/$2.$3

How would I go about creating a mod_rewrite that redirects to launch.php?i=/the/url/that/they/want?

So if the user types mydomain.com/dashboard, the document the server actually sends them is /launch.php?i=/dashboard.
The one caveat is that I would like to leave requests for
/flags
/people
/posters
/css
/icons
/images
/libraries
/patterns
alone, and they should request the actual folder.
How would I create such a mod_rewrite?
This is the .htaccess file for the CakePHP Framework.
Please replace the index.php and ?url= to fit your needs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
The "!-d" tells Apache to follow existing folders and "!-f" to follow existing files.
Everything else is channelled through index.php
As suggested in a comment, you have to be aware that if it's not working it could be because mod_rewrite is not enabled and you'll not get an error stating that fact, you'll probably only have a HTTP 404.

How do I ignore a directory in mod_rewrite?

I'm trying to have the modrewrite rules skip the directory vip. I've tried a number of things as you can see below, but to no avail.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteRule ^vip$ - [PT]
RewriteRule ^vip/.$ - [PT]
#RewriteCond %{REQUEST_URI} !/vip
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How do I get modrewrite to entirely ignore the /vip/ directory so that all requests pass directly to the folder?
Update:
As points of clarity:
It's hosted on Dreamhost
The folders are within a wordpress directory
the /vip/ folder contains a webdav .htaccess etc (though I dont think this is important
Try putting this before any other rules.
RewriteRule ^vip - [L,NC]
It will match any URI beginning vip.
The - means do nothing.
The L means this should be last rule; ignore everything following.
The NC means no-case (so "VIP" is also matched).
Note that it matches anything beginning vip. The expression ^vip$ would match vip but not vip/ or vip/index.html. The $ may have been your downfall. If you really want to do it right, you might want to go with ^vip(/|$) so you don't match vip-page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This says if it's an existing file or a directory don't touch it. You should be able to access site.com/vip and no rewrite rule should take place.
The code you are adding, and all answers that are providing Rewrite rules/conditions are useless! The default WordPress code already does everything that you should need it to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Those lines say "if it's NOT an existing file (-f) or directory (-d), pass it along to WordPress. Adding additional rules, not matter how specific or good they are, is redundant--you should already be covered by the WordPress rules!
So why aren't they working???
The .htaccess in the vip directory is throwing an error. The exact same thing happens if you password protect a directory.
Here is the solution:
ErrorDocument 401 /err.txt
ErrorDocument 403 /err.txt
Insert those lines before the WordPress code, and then create /err.txt. This way, when it comes upon your WebDAV (or password protected directory) and fails, it will go to that file, and get caught by the existing default WordPress condition (RewriteCond %{REQUEST_FILENAME} !-f).
In summary, the final solution is:
ErrorDocument 401 /misc/myerror.html
ErrorDocument 403 /misc/myerror.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I posted more about the cause of this problem in my specific situation, involving Wordpress and WebDAV on Dreamhost, which I expect many others to be having on my site.
You mentioned you already have a .htaccess file in the directory you want to ignore - you can use
RewriteEngine off
In that .htaccess to stop use of mod_rewrite (not sure if you're using mod_rewrite in that folder, if you are then that won't help since you can't turn it off).
Try replacing this part of your code:
RewriteRule ^vip/.$ - [PT]
...with the following:
RewriteCond %{REQUEST_URI} !(vip) [NC]
That should fix things up.
RewriteCond %{REQUEST_URI} !^pilot/
is the way to do that.
In my case, the answer by brentonstrine (and I see matdumsa also had the same idea) was the right one... I wanted to up-vote their answers, but being new here, I have no "reputation", so I have to write a full answer, in order to emphasize what I think is the real key here.
Several of these answers would successfully stop the WordPress index.php from being used ... but in many cases, the reason for doing this is that there is a real directory with real pages in it that you want to display directly, and the
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
lines already take care of that, so most of those solutions are a distraction in a case like mine.
The key was brentonstrine's insight that the error was a secondary effect, caused by the password-protection inside the directory I was trying to display directly. By putting in the
ErrorDocument 401 /err.txt
ErrorDocument 403 /err.txt
lines and creating error pages (I actually created err401.html and err403.html and made more informative error messages) I stopped the 404 response being generated when it couldn't find any page to display for 401 Authentication Required, and then the folder worked as expected... showing an apache login dialog, then the contents of the folder, or on failure, my error 401 page.
I’ve had the same issue using wordpress and found that the issue is linked with not having proper handler for 401 and 403 errors..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These conditions are already supposed not to rewrite the url of existing folders but they don’t do their job for password protected folders. In my case, adding the following two lines to my root .htaccess fixed the problem:
ErrorDocument 401 /misc/myerror.html
ErrorDocument 403 /misc/myerror.html
Of course you need to create the /misc/myerror.html,
This works ...
RewriteRule ^vip - [L,NC]
But ensure it is the first rule after
RewriteEngine on
i.e.
ErrorDocument 404 /page-not-found.html
RewriteEngine on
RewriteRule ^vip - [L,NC]
AddType application/x-httpd-php .html .htm
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
etc
I'm not sure if I understand your objective, but the following might do what you're after?
RewriteRule ^/vip/(.*)$ /$1?%{QUERY_STRING} [L]
This will cause a URL such as http://www.example.com/vip/fred.html to be rewritten without the /vip.