Trailing slashes problem - apache

When I type this "http://example.com/Hello%20There/" , it displays the
index page wich is : "http://example.com/Hello%20There/index.html" .
Well, what I want to do is when the user types "http://example.com/Hello%20There"
(so like the first one except it doesn't have a trailing slash).
I tried many things and specially regular expressions, but nothing works because I think
that the server stops the reg exp process when he finds a space ("%20" in the URL).
I tried this reg exp:
Options +FollowSymLinks
rewriteEngine On rewriteCond %{REQUEST_URI} ^(.*)\ (.*html)$
rewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]
rewriteCond %{ENV:space_replacer}!^$
rewriteCond %{ENV:space_replacer}!^.*\ .*$
rewriteRule ^.*$ %{ENV:space_replacer} [R=301,L]
and also put:
DirectorySlash On
in the "mod_dir" module of Apache.
So, my question is:
- How to tell to the server to add a trailing slash when the user types an url
without a trailing slash;$

You can make a character optional by appending the ? quantifier to it like this:
RewriteRule ^([^/]+)/?$ $1/index.html
Now both /foobar and /foobar/ would be rewritten to /foobar/index.html.
But it would be better if you use just one spelling, with or without the trailing slash, and redirect the other one:
# remove trailing slash
RewriteRule (.+)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$1/ [L,R=301]
These rules either remove or add a missing trailing slash and do a permanent redirect.

I had the same problem, but I was using mod_alias to set up a subsite. Turns out, I needed to make a second alias without the trailing slash so that it would work correctly. Looked something like this:
Alias /forum/ "/var/www/forum"
Alias /forum "/var/www/forum"
<Directory "/var/www/forum">
Options FollowSymlinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
In Ubuntu, I had to edit the /etc/apache2/mods-enabled/alias.conf file with these lines, then restart apache. Couldn't find this answer anywhere on the web; I just stumbled onto it myself as mod_rewrite wasn't working and the DirectorySlash command didn't help either. I was appending a non-Drupal program as a subsite under a Drupal installation, which is what kicked off all this madness in the first place...

Don't use trailing slash to define an alias.
Both URLs http://example.com/myalias1 and http://example.com/myalias1/ would work fine.
Example:
sudo vi /etc/apache2/apache2.conf
Alias /myalias1 "/path/to/folder1"

Related

apache rewriterule for tilda ~user folder

I'm trying to get domain.com/~userX folders working without using userdir.
It works fine when the ~userX folders are in the server doc root doc folder, but there's too many and need to put them in a sub folder, ie /homepages so the best I've gotten is in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^~ "/homepages/%{REQUEST_URI}"
</IfModule>
Options -Indexes
With the trailing slash it works fine:
domain.com/~userX/
Without the trailing slash it automatically redirects to:
http://ddomain.com/homepages/~userX/
Which I don't want, so I thought I would turn directory slash off:
<Directory /var/www/domain/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
DirectorySlash Off
</Directory>
The trailing slash still works but without the trailing slash I now get:
Forbidden
You don't have permission to access this resource.
error log:
Cannot serve directory /var/www/domain/public_html/homepages/~userX
This is over my head, I've search for a few days and I'm really stuck :/
Any help much appreciated thanks.
1st Method:
Try:
RewriteRule ^(~[^\/]+)(?:\/(.*)|$) /homepages/$1/$2 [L]
The trailing slash is present or not, it internally redirects with the trailing slash.
Issues:
I think, if the user enters an URL with a slash in a sub-directory of the /homepage directory, it is probably not going to work.
2nd Method:
Try:
RewriteRule ^(~[^\/]+)$ /$1/ [R=302,L]
RewriteRule ^~ /homepages/%{REQUEST_URI} [L]
3rd Method:
This might work better than the first and the second solution
RewriteRule ^(~[^\/]+)(?:\/(.*)|$) /homepages/$1/$2 [PT,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^\/])$ /$1/ [L]

My .htaccess rule is not working

I want all my URL with matching pattern as
/released/2013/iron-man
/released/2013/abc-of-death
/released/2012/saw6
to be redirected to
/released/1.php
and I can the name as well.
I am adding this rule to my .htaccess file but its not working
RewriteRule ^released/([0-9]+)/?$ /released/1.php?id=$1 [L]
The trailing question mark matches an optional ending / which is not what you want.
^released/([0-9]+)/iron-man$
or
RewriteRule ^released/([0-9]+)/(.+)$ /released/1.php?id=$1+$2
Problem is that you have $ after second slash but you have movie name after 2nd slash like iron-man etc. Remove $ since you are not matching it.
Make sure that mod_rewrite and .htaccess are enabled through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(released)/([0-9]+)/ /$1/1.php?id=$2 [L,QSA,NC]
A RewriteRule which does not specify a specific external domain is always executed internally. To make it redirect as you ask add [R=301] at the end - or 302, 303 or 307 depending on which kind of redirect you require, but usually 301 is fine.
Besides that, the regular expression you wrote does not allow for extended URLs - remove the trailing $. After that the /? part is moot so you can remove it as well.
The resulting line would read:
RewriteRule ^released/([0-9]+) /released/1.php?id=$1 [L,R=301]

Stripping trailing slash in URL doesn't work in subdirectory

The following rule works to remove the trailing slash for all pages in my web root:
RewriteRule ^(.+)/$ /$1 [L,R=301]
However, it does not work when placed in a sub folder. I've also tried:
RewriteRule (.*)/$ /$1 [L,R=301]
to no avail. What happens is, it redirects
http://example.com/testfolder/testpage/
to
http://example.com/testpage
What am I missing? Thanks in advance!
Have you tried omitting the leading slash from your replacement?
RewriteRule ^(.+)/$ $1 [L,R=301]
It's just a stone's throw from what you have. The context of the directory rewrite (being "in" /testfolder) may be the root cause of the trouble.
you may be able to define this in variables also make sure Rewrite mods are enabled in your php.ini if there is no other way of doing what you need.
The trailing slash fixup is being done by mod_dir. Rewrites in your per-directory context are re-injected into the URL processing chain and subject to the fixup again.
The behavior is configurable. E.g.
<Directory /path/to/wherever>
DirectorySlash Off
...
</Directory>
The context for this is not only Directory: it is server config, virtual host, directory, .htaccess.

apache RewriteRule requires trailing slash at the end of url to work

Ok so i have a url like
domain.com/item/item_id/item_description/page
when i type the link without
/page
on the url it throws a 404 error and i have to type the trailing slash on the url to make it work..
this is my htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^item/([0-9]+)/(.*)/(.*)/?$ item.php?action=item&id=$1&desc=$2&page=$3
i have found this after searching:
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
which kinda solves my problem but how can i make the trailing slash to be optional by the user if the user wants to add it or not so it wont redirect everytime a slash is not found
You can handle the request using one rewriterule.
RewriteRule ^item(?:\.php)/([0-9]+)/([^/]+)?/?([^/]+)?/?$ item.php?action=item&id=$1&desc=$2&page=$3 [L]
Please note I have added (?:\.php) before ^item, just to be sure this rewriterule works, if your webserver for some reason convert request
domain.com/item/...
into
domain.com/item.php/...
Tip: you can see your current rewriterule behavior enabling RewriteLog:
RewriteLogLevel 9
RewriteLog "/var/log/apache2/dummy-host.example.com-rewrite_log"
Be careful do not use this in production.
Use two rewrite rules (one with and one without "page"):
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^item/([0-9]+)/(.*)/?$ item.php?action=item&id=$1&desc=$2
RewriteRule ^item/([0-9]+)/(.*)/(.*)/?$ item.php?action=item&id=$1&desc=$2&page=$3

.htaccess same url with or without /

I am doing a painful rewrite of many urls on a website I am currently working on, but I have noticed a small problem:
RewriteRule ^domains/transfer$ ./cart.php?gid=11 [L,NC]
This line with navigate if I go to:
http://my-site/domains/transfer
But it won't work with a trailing /:
http://my-site/domains/transfer/
Is there a way I can change the RewriteCode to take either argument and go to the same page. It seems wasteful to have the same line twice to include a '/'
Any help would be appreciated.
Cheers
Change the line to this:
RewriteRule ^domains/transfer/?$ ./cart.php?gid=11 [L,NC]
The magic is here: /? and that allows for the preceding character, in this case the slash (/), to be optional.
If you want something to come after the transfer, then remove the dollar sign ($) which marks the end of the allowable matching.
I would recommend you to allow just one URL form, the one with or without the trailing slash, and redirect if malformed:
# remove trailing slash
RewriteRule (.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]