add file-ending using .htaccess? - apache

I'm trying to add a file-ending (.kml) to a url using .htaccess but it's not working
The url is http://resihop.nu/kml
I want it to be http://resihop.nu/kml.kml this is what i tried:
RewriteRule ^kml.kml$ kml
Here is my full .htacess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
RewriteRule ^kml\.kml$ kml

You need to tell the server how to handle .kml files. Add this line to your .htaccess:
AddType application/x-httpd-php .kml
(Assumes you want .kml files to be parsed as PHP.)
Edit: Never mind, I just re-read your question and I realise that's not your problem. Just ignore me!

This should work for your case of using your own extension:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.kml$ /$1.php [NC,L]
Then you could access any file with the .kml extension, such as hello.kml.
Hope this helps.

Related

How to remove extension .php in subdirectory

I have problem to remove extension .php. For example the url is like
https://www.domainname.net/dev/member.php. I just want to remove the extension .php. can someone help me.
Try this in your htaccess :
Options +Multiviews
then you will be able to access your file without extension.
Here is the .htaccess code snippet that will help you put this code in your .htaccess file:
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>

1&1 web hosting not reading my .htaccess file

It seems that my web host (1&1) is not reading my .htaccess file.
I tried configuring my .htaccess file to prevent file access via the web browser, specifically all files with the following extensions: .txt, .jpg, .jpeg, .pdf, .xlsx, .xls, .doc, .docx, .eps, .gif, .png.
Here is my .htaccess file:
#DirectoryIndex index.php index.html
#Options +FollowSymLinks
Options -Indexes
# 1und1 needed for php extensionless redirect.
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /relative/web/path/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$index.php [L,QSA]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.ltd [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.ltd.*$ [NC]
RewriteRule \.(gif|jpg|js|txt|php|html|css|js)$ /messageforcurious [L]
</IfModule>
<FilesMatch "\.(txt|jpg|jpeg|pdf|xlsx|xls|doc|docx|eps|gif|png)$">
Deny from all
</FilesMatch>
#php_value register_globals 0
How do I achieve what I want? Any tips?
It seems that the .htaccess file is readable by 1&1, but there is an easier way to block access to all files than what you are doing.
If you truly want to block all people from visiting your website - which is what blocking all files will do - you can make your .htaccess into:
Deny from all
Otherwise, you can do this, which will allow only some files to be accessed (in this case, only .html files):
<FilesMatch "\.html$">
Allow from YOUR_IP_ADDRESS
Deny from all
</FilesMatch>
For more reading, I'd look at this 1&1 overview of their .htaccess file
Note: I am entirely unsure of what your index.php redirects are doing above, since it seems that you want them to go to /messageforcurious. I'd remove those index.php redirects since it seems you don't want them.

php-fpm - How to execute certain symlinks as PHP scripts

I am running Apache 2.2 with FastCGI and php-fpm. I am trying to duplicate the following logic:
<FilesMatch "^(admin|api|app)?(_dev)?$">
#ForceType application/x-httpd-php
SetHandler php-fcgi
</FilesMatch>
Which allows me to symlink admin.php as admin, so I can remove the .php extension. It seems the only way to do this with php-fpm is to set the security.limit_extension of the www.conf file to empty, however, as the comments indicate, this is a pretty big security hole, in that php code can now be executed from within any file, regardless of extension.
What would be the preferred way to accomplish the above, but still maintain some semblance of security?
Looks like the best solution so far is to manually add the known symlinks to the list (located in /etc/php-fpm.d/www.conf):
security.limit_extension php admin admin_dev api api_dev app app_dev
Not sure if security.limit_extension directive can even take a regex, doesn't look like it, so this is about as good as it gets. As mentioned in the OP, you will still have to maintain the filesmatch directive in the vhost config as well:
<FilesMatch "^(admin|api|app)?(_dev)?$">
SetHandler php-fcgi
</FilesMatch>
-- Update --
Per the comments by tftd, adding current rewrite directive:
RewriteBase /
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
#Mike, based on your updated answer, something similar to this .htaccess file should be able to handle what you're trying to do:
# Enable the rewrite engine
RewriteEngine on
# Set the rewrite base path (i.e. if this .htaccess will be running at root context "/" or a subdir "/path")
RewriteBase /
# If the file exists, process as usual.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [NC,L]
# If the dir exists, process as usual (if you don't need this, just comment/remove the next two lines).
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [NC,L]
# If (requested_file_name).html exists, rewrite to that file instead.
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [QSA,L]
# If (requested file name).php exists, rewrite to that file instead.
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.html [QSA,L]
# If none of the above rules were triggered, fallback to index.php.
RewriteRule ^(.*)$ index.php [QSA,L]
With a bit of tweaking this should be able to do the job without the need of having to dive into httpd.conf and the <VirtualHost> nor <FilesMatch> directives. Hope this helps.

Mod_Rewrite rewriting directory I dont want it to

So I am using Kohana which is useful if you know it, but not needed to assist me.
I have the following mod_rewrite rules:
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/store/.*$
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php [L]
So I am trying to rewrite all requests for files and directories that do not exist to index.php.
However, I want any request sent to mydomain.com/store/* to go through as there is another htaccess file in the store directory that does work there. That does not seem to be working at the moment. Any ideas?
Full htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
#ErrorDocument 404 http://www.mydomain.com/404Page.html
#Options +FollowSymlinks
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
RewriteCond %{REMOTE_ADDR} !^myip
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://www.mydomain.com/maintenance.html [R=307,L]
##301 Redirect Rules##
#some 301 redirects i did not include here
##Kohana Redirect Rules##
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system|kohana|vendors)\b.* http://www.mydomain.com/ [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?store/
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php [L]
Try this condition:
RewriteCond %{REQUEST_URI} !^/?store/
There is no need to check the characters after the directory. I made the first slash optional if I remeber correctly is the first slash only visible if your server configuration does not contain a tailing slash.
The issue turned out to be in the .htaccess file in the store directory not the one in the webroot. Thanks all, and sorry for my stupidity. IF anyone wants to leave comments on how to debug something of this nature for future users that would be awesome.

removing forward slash in .htaccess

I am using Jobbersbase for my online job portal. In which i have given link to my main webpage page like this http://www.mydomain.com/aboutus.html, but its not working because the link is taking '/' at the end http://www.mydomain.com/aboutus.html/
I tried adding RewriteCond %{REQUEST_FILENAME}\.html -f in .htaccess , if i add that other links doesnt work which has / for example http://www.mydomain.com/jobs/
Now my .htaccess looks like this
# AddType x-mapp-php5 .php
# AddHandler x-mapp-php5 .php
RewriteEngine On
RewriteCond %{REQUEST_URI} .*/$
RewriteRule (.*)/$ $1
ErrorDocument 404 /page-unavailable/
<files ~ "\.tpl$">
order deny,allow
allow from none
deny from all
</files>
Someone please suggest me how to do it
thanks
Perhaps it isn't as easy as I imagine, but:
RewriteEngine On
RewriteCond %{REQUEST_URI} .*/$
RewriteRule (.*)/$ $1
With modifications for the specific files/directories you care about, if necessary. See below:
RewriteEngine On
RewriteCond %{REQUEST_URI} .*aboutus\.html/$ [or]
RewriteCond %{REQUEST_URI} .*contact\.html/$
RewriteRule (.*)/$ $1
This should strip trailing slashes from those specific pages only. Unfortunately I can't verify this because I'm having trouble getting my .htaccess working with my virtual host configuration.