removing forward slash in .htaccess - apache

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.

Related

How to remove `/index.php` from all urls, including the last trailing slash?

I've been looking around the web and especially here on stackoverflow for THE answer to this question.
I only use directories with an index.php file in it. Includables and private stuff are outside the public_html (cpanel) directory.
By using directories, anchor links and queries will look like:
http://domain.com/sub/#anchor or http://domain.com/sub/?query
And I just don't like it. People keep telling me that this is irrelevant for SEO but I don't think that this is the case. Firstly because I want consistency, secondly, trailing slashes creates duplicates thus I need 301 redirects! Consistency and duplicates are indeed a SEO problem!
This is just an example of how my website is structured:
/
·--index.php
|
·--/about-us/
| |
| ·--index.php
·--/contact-us/
|
·--index.php
Users will never know that about-us is a directory, and they won't type the last trailing slash anyway. This creates duplicates and HTTP errors.
On the web I've found only non-working examples, and as for what I've understood, I have to internally add the trailing slash and index.php. This helps to avoid security problems and makes all the thing work!
Here, and here I asked something similar. But in that case I had to create the /public directory. Now I am managing to change host, and I will be able to use the non public directory to store php files.
Since I don't need the /public directory anymore, I copied part of that code and pasted it on a new .htaccess.
Here is the .htaccess
# Options
Options +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php index.html
DirectorySlash off
# Enable Rewrite Engine
RewriteEngine on
RewriteBase /
# www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %1##%{HTTPS}s ^(.+)##(?:on(s)|)
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301,NE]
# remove trailing slash from all URLs
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+)/$ /$1 [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# internally add trailing / to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
<FilesMatch ^\.>
order allow,deny
deny from all
</FilesMatch>
<Files *.inc>
order allow,deny
deny from all
</Files>
The code seems to work well in subdirectories but not in root. I guess that this is because the code above was tailored to work on the /public subdirectory.
How can I make it work on root too?
To sum-up, I need 301 redirects, otherwise there will be duplicates of content!!
http://www.domain.com/ R--> http://domain.com/
http://domain.com/ R--> http://domain.com
http://domain.com/sub/ R--> http://domain.com/sub
http://domain.com/sub/index.php R--> http://domain.com/sub
http://domain.com/sub/index.php#anchor R--> http://domain.com/sub#anchor
http://domain.com/sub/#anchor R--> http://domain.com/sub#anchor
You can use these rules to meet all the requirements including removal of index.php. Do remember that anchors cannot be preserved on server side as server won't even get #anchor in the HTTP request i.e. only http://domain.com/sub/ will be received in Apache logs.
# Options
Options +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php index.html
DirectorySlash off
# Enable Rewrite Engine
RewriteEngine on
RewriteBase /
# www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %1##%{HTTPS}s ^(.+)##(?:on(s)|)
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301,NE]
# remove index.php
RewriteCond %{THE_REQUEST} \s/*(/.*)?/index\.php[?\s] [NC]
RewriteRule ^ %1 [L,R=301,NE]
# remove trailing slash from all URLs
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+)/$ /$1 [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# internally add trailing / to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
<FilesMatch ^\.>
order allow,deny
deny from all
</FilesMatch>
<Files *.inc>
order allow,deny
deny from all
</Files>
Don't trim trailing slashes, you're fighting the normal directory structure of URLs. There is no such URL as http://domain.com even if your browser hides it, the slash is still being sent in the request and will be added back to a URL copied from an address bar.
Doing a redirect for a single domain is good. Automatic 301 redirects (the kind that Apache does) from http://domain.com/foo to http://domain.com/foo/ are good for SEO. How are index.php strings in your URLs in the first place? There's no need for them to be there; make sure all your own links are free of them. The easiest way to ensure only unique pages are indexed is with <link rel=canonical href="…">.

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.

Redirect everything BUT index.php with htaccess

I have a dilemma with a site I own and how to configure the .htaccess file.
I just to had a forum in mysite.com, since I wanted to add a blog I put a 301 redirect and move everything to a dir mysite.com/forum/, all good there.
But now I want to had a different index page so people can choose between go to the blog (blogs actually) or go to the forum.
I trying to avoid losing several pages already indexed by web searchers.
Right now the htaccess file in the root of my site looks like:
Options +FollowSymLinks
RewriteEngine on
<Files 403.shtml>
order allow,deny
allow from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirigir el dominio sin www a www
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,QSA,L]
RewriteRule (.*) http://www.mysite.com/forum/$1 [R=301,L]
</IfModule>
Is there a way to put something like
If request URL = root then index.php
Else http://www.mysite.com/forum/$1
Thanks for your help
Cheers!
You probably want something like
# if request = root
RewriteRule ^/?$ /index.php [L]
# else
RewriteRule !^/?(index\.php)?$ http://www.mysite.com/forum%{REQUEST_URI} [L,R=301]
To replace the last rule that you have

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.

add file-ending using .htaccess?

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.