How do I remove a trailing slash? - .htaccess

I just want to remove a trailing slash from a directory. For example I want /p/page/ to show up as /p/page. It just looks better, doesn't it?
However I've tried many different kinds of mod_rewrites but none have worked or something happened.
I just want this to apply to subfolders (even better, any slash in a folder in a folder in a folder like /a/b/c), not /p/ as this may affect other parts of my site in a negative way.

You can try adding the following line to your .htaccess file:
DirectorySlash Off
That solved the problem for me a while ago. Of course if the path is only / I don't think you can get rid of it.

Copy this code in your root .htaccess file (directly under DOCUMENT_ROOT):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L]
DirectorySlash Off
RewriteCond %{THE_REQUEST} ^GET\s(.*)/\s
RewriteRule ^ %1 [R=302,NE,L]
It will externally redirect http://localhost/blog/ to http://localhost/blog while still displaying default index.html or index.php or whatever under /blog directory.

Related

.htaccess subdomain Rewrite rule is not working

I am making a website builder an I would like to make urls prettier.
The url would be for example:
https://ccc-bb.example.com => https://example.com/project/show/ccc/bb
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# prevents files starting with dot to be viewed by browser
RewriteRule /\.|^\.(?!well-known/) - [F]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-(.*)$ https://example.com/project/show/$1/$2 [L]
When I use above (https://ccc-bb.example.com) it sends me to the subdomain empty folder. The folder has only the .htaccess file.
What am I missing? I've never edited an .htaccess file and Google didn't help me (or I don't know what should I looking for).
Your first rule for dotfiles is okay but would be better the other way around, since the second part can only match the start, but the first can only match in subdirectories.
RewriteRule ^\.(?!well-known/)|/\. - [F]
Your other rule's problem is that you are expecting it to match the subdomain. RewriteRules do not operate on the entire string you see in your browser's address bar, only the path part, and in .htaccess they see even less as the leading directory is stripped off, too. To access the info you want, use a RewriteCond:
RewriteCond %{HTTP_HOST} ^([^-]++)-([^-.]++)\.example\.com$
RewriteRule ^(?!project/show/).* project/show/%1/%2/$0 [L,DPI]
(You don't need to include \.example\.com$ if your main domain contains no hyphens.)

How to remove subdirectories and replace file extensions with a trailing slash

The following is my directory structure
Root (example.com)/
index.htm
contact.htm
privacy.htm
disclaimer.htm
cat/
play/
fun.htm
rest/
sleep.htm
I managed to remove the file extension and add a trailing slash with:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R,L]
But I also want to make it in such a way that when people go to www.example.com/fun/ they're able to access www.example.com/cat/play/fun.htm without redirecting, which means, in the address bar it still shows www.example.com/fun/.
I know I can use the direct approach like:
RewriteRule ^fun/$ /cat/play/fun.htm [L]
RewriteRule ^sleep/$ /cat/rest/sleep.htm [L]
But I'll be adding more files to these 2 subdirectories (/cat/play/ and /cat/rest/), so I was wondering if there's a single rewrite rule to perform the rewrites for these files instead of having to enter 100 rewrite rules for 100 files under those 2 subdirectories. Please enlighten.
Appreciate your help.
Put these lines in .htaccess
RewriteRule ^fun/?$ play/fun.htm [L]
RewriteRule ^sleep/?$ rest/sleep.htm [L]
They cannot be moved to one rule, so they should be used with separate rules for each URL.

htaccess 404 issue

I am working with a CMS, and up until today its been fine.
But i have discovered that the mod rewirte only works if the website is in the root directory. If I put the entire CMS into a folder, i get a 404.
Please help!
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([^/\.]+)/?$ /index.php?1=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6&7=$7 [L]
Then you need to add the subfolder to your rewrite rule like this, for all of your rules.
RewriteRule ^([^/\.]+)/?$ /subfolder/index.php?1=$1 [L]
With your configuration apache will search for the index.php file in the root directory
Your script is extracting data that are between slashes like:
/test1/test2/ transfers into /index.php?1=test1&2=test2
but since you started using folder it works like:
/folder/test1/test2/ transfers into /index.php?1=folder&2=test1&3=test2
so the folder name is breaking your structure, you will need to fix every line with folder name or expression like this:
RewriteRule ^[^/\.]+/([^/\.]+)/?$ /folder/index.php?1=$1 [L]
So you will need to fix both regular expression and new path on every line, just replace folder with name of your folder and add [^/\.]+/ at start
If that doesnt work you may just need to fix regular expression only without adding /folder to the second part of line

htaccess rewrite showing redundant root folder

I've searched but haven't found a solution to this exact problem.
I have a website domain 'mainwebsite.com' and several other top-level-domains managed in the htaccess. The issue I'm having is with 'alternatedomain.com' and subdirectories.
#alternatedomains
RewriteCond %{HTTP_HOST} alternatedomain.com
RewriteCond %{REQUEST_URI} !/alternatedomain/
RewriteRule ^(.*)$ alternatedomain/$1 [L]
This works fine, alternatedomain.com happily serves up mainwebsite.com/alternatedomain .
The problem is when I have access any folder like
alternatedomain.com/folder
, the URL in the title bar changes to
alternatedomain.com/alternatedomain/folder/
and it's driving me crazy. Can't figure out what I'm doing wrong!
EDIT: It only happens when I try access alternate.com/folder, and not when there is a suffix /, so alternate.com/folder/ works fine, without the redundant folder name
Your help is most appreciated!
This is because mod_dir (that adds the /) does not know the url is rewritten.
You could just disable DirectorySlash with
DirectorySlash Off
This will however make requests to alternatedomain.com/folder result in a 404 not found. You can fix this with some rewriterule though. So the complete solution would be something like:
DirectorySlash Off
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^alternatedomain/(.+[^/])$ /$1/ [R,L]

Remove trailing slash with mod_rewrite

I've tried every single example I could find, they all produce an internal server error. I have these rules set up (this works, no error):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^((/?[^/]+)+)/?$ ?q=$1 [L]
So if it's not an existing file or an existing directory with an index.php we redirect. For instance, http://domain.com/foo/bar becomes http://domain.com/?q=foo/bar
Thing is, I want the trailing slash stripped. So take off the /? at the end of the rule. How do I make it so that http://domain.com/foo/bar/ becomes http://domain.com/foo/bar with a visible redirect first (fixing the client's URL), and only then the real, silent redirection to ?q=?
Everywhere I look I see this:
RewriteRule (.*)/$ $1 [R,L]
But it gives me a 500 error if I insert it before my rule.
If foo/bar exists as a real directory, then the server will be redirecting the client to foo/bar/ (with the trailing slash). It has to do that in order for relative URLs to work correctly on the client. If you put in a rule to rewrite that back to foo/bar with a redirect then there will be a loop. An easy way to test if that's happening is to specify a path that doesn't exist at all (I assume from your index.php detection that the directory tree actually exists). The nonexistent path won't trigger the built-in redirect.
If I setup a similar set of rules to yours (plus the suggested slash-removal rule) I can see the difference between a directory that exists and one that doesn't. The ones that don't work as expected, the ones that do cause Firefox to say This page isn't redirecting properly. IE8 says something similar. Perhaps the Apache setup you're using can detect it and turns it into the 500 error?
It looks like the simpler rewrite rule you mention at the end of your question should work. The problem is, the 500 error isn't really helpful in figuring out why it's not working. One way I've found useful in helping debug mod_rewrite errors is to enable it's logging. Add the following to your httpd.conf:
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
RewriteLogLevel 3
Then try again, and look in the log to see what's going on. Once you're done, you can disable the log be setting the rewriteloglevel 0. See the mod_rewrite docs for details.
Try this rule in front of your current rule:
RewriteRule (.*)/$ /$1 [R,L]
Try these rules:
#prevent mod_dir from adding slash
DirectorySlash Off
#redirect /folder/ to /folder
RewriteCond %{THE_REQUEST} ^GET\s\S+/(\?\S+)?\s [NC]
RewriteRule ^(.*)/$ /$1 [R=301,L,QSA]
#internal redirect for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L]

Resources