Access Drupal7 site in a subdirectory without trailing slash - php

I have a Drupal7 site hosted in a sub-directory and site can be access following with directory path and trailing slash:
http://mydomain.com/dir1/dir2/ (with the trailing slash).
I’m looking to access this site without the trailing slash:
http://mydomain.com/dir1/dir2
I tried with the DirectorySlash Off in the .htaccess file. But it gives Access forbidden error when accessing site without trailing slash.
Then I created a new .htaccess file in the ../dir1 and tried to rewrite the url to get the main page content when user access site without trailing slash.
<IfModule mod_rewrite.c>
DirectorySlash Off
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^dir2$ dir2/index.php
</IfModule>
Again I’m getting the same issue “Access forbidden”. Is there anything to do in .htaccess file in Drupal root after setting these rules?
Any idea how to access Drupal site hosted in a sub-directory without trailing slash?

Try this. But I'm not tested it.
Basically the trick here is to use a 301-redirect to remove the trailing slash at the end of all your URL’s. The code to put in your .HTACCESS
#remove trailing slashes
RewriteCond %{HTTP_HOST} !^\.domain\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
There is a Drupal plugin also. According to their description looks like it will match to your requirement. Give a try.
https://drupal.org/project/globalredirect

Related

RewriteRule - discard trailing slash

I have a PHP project with a .htaccess file:
Options FollowSymLinks
RewriteEngine On
RewriteBase "/blog/"
RewriteRule "^([^.?/]+)/?$" "index.php?page=$1" [END,QSA]
I have a very strange problem with url localhost/blog/administration. It's definitely not caused by a PHP file, because happens even with empty source code.
My problem is: when I type localhost/blog/administration, the URL in address bar is changed to localhost/blog/administration/?page=administration. This doesn't happen after typing localhost/blog/administration/ (with trailing slash) in the address bar.
What's stranger - given problem happens only with administration URL but not with, for example, home.
What could be the cause?
DISCLAIMER: Said problem happens even when I use fresh browser (no cache) and index.php with exit; at the beggining...
Trailing slash is added at end of URI if that points to a real directory by Apache's mod_dir module for security reasons otherwise it may result in directory listing of your directory.
It is advisable to have index.php inside your /blog/administration/ directory to prevent this behavior. Otherwise you can have a separate rule to add trailing rule before final rule:
Options FollowSymLinks -Indxes
RewriteEngine On
RewriteBase /blog/
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^./]+)/?$ index.php?page=$1 [END,QSA]
Also take note of -Indxes in options line above to disable directory listing.

url rewriting with .htaccess

I'm trying to rewrite the url on a site I have on localhost (port 8000), I have already set this up for certain other directorys but I can't manage to do it for this one. I compared it to several other working .htaccess files but there wasn't any difference. (The module is activated.) The site is in a subdirectory called HTF and the file single.php works just fine. The htaccess file is placed in the same directory.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule videos/([0-9]+)/$ /HTF/single.php?id=$1 [L]
I would like to get an url like localhost:8000/HTF/video/1232434 for exemple.
Any help appreciated, tested several types of htacces files, here a working exemple for a other subdirectory called sorted:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^image-post/([0-9]+)/(.+)\.php$ /sorted/image-post.php? id=$1&title=$2 [L]
RewriteRule ^pages/([0-9]+)/(.+)\.php$ /sorted/page.php?page_number=$1&cat=$2 [L]
Your rule will match videos, not video as you were wanting. Also it will only match with a trailing slash.
This will work
RewriteEngine on
RewriteRule ^video/([0-9]+)/?$ single.php?id=$1
localhost:8000/HTF/video/1232434
will not match
videos/([0-9]+)/$
because of the trailing slash /. Try /? to make the slash optional or leave away. And videos.
^/HTF/video/([0-9]+)/?$ /HTF/single.php?id=$1 [L]
or
^/HTF/video/([0-9]+)$ /HTF/single.php?id=$1 [L]
This shall do the job. Sorry can not try here.

htaccess RewriteRule doesn't work properly if no trailing slash

I am trying to redirect one subdirectory to another using RewriteRule in the main directory's .htaccess file.
For example: http://website.com/subdir should rewrite to http://website.com/another_dir/destination_dir but the user should still see http://website.com/subdir in the address bar.
This works perfectly if the user ends the URL with a trailing slash. Example: http://website.com/subdir/ (Generates a 200
However, if the slash is omitted, a 301 redirect is generated and we see the undesired destination directory. For example, http://website/subdir redirects the user to http://website/another_dir/destination_dir
Here are the pertinent parts of .htaccess:
# URL Rewriting:
RewriteEngine On
RewriteBase /
...
# Redirect subdirectories:
RewriteRule ^subdir(.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]
Any assistance is appreciated!
You can adjust the regex in your rewrite rule to optionally match a slash.
RewriteRule ^subdir(/?.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]
Note the /? just after subdir. This says that there may or may not be a slash just after your subdir, so the regex will match either way.
A quick fix would be
DirectorySlash OFF in this htaccess, and DirectorySlash ON a .htaccess in another_dir/

Mod Rewrite, ignore directory unless URL has a trailing slash

I am making an url shortener with a base 64 encoding for my urls. problem is I also have a bunch of folders inside my main directory. My directory looks something like this
/
/css
style.css
/handlers
handle_database.php
index.php
.htaccess
And this is the rewrite rule I use to capture the encoded urls
RewriteRule ^([A-Za-z0-9_\-]{3,8})$ index.php?a=$1
And that works. my problem comes when my system generates an url that would be like this http://exampleurlshortener.com/css, in an ideal scenario the rewrite rule would capture the "css" and let my index.php handle the rest, problem is apache adds a trailing slash and it ends up getting inside the css directory.
So what I need is that
http://exampleurlshortener/css -> lets index.php handle the request
http://exampleurlshortener/css/ -> access the actual directory
So far I've had no luck, because apache keeps adding the trailing slash
You need to turn off DirectorySlash to avoid Apache adding a trailing slash. It is also important to turn off Indexes option to avoid Apache presenting directory listing to users.
DirectorySlash off
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^([\w-]{3,8})$ index.php?a=$1 [L,QSA]

How do I get rid of the extra slash at the end of the url

I have this link and as you can see there is an extra / at the end. Basically i have a restaurant_pos folder with an index.php file in it but apache is adding the extra slash. I thought there was a way to do this in htaccess with directory directives off or something like that...not sure the name...any ideas
This should do the job for you (redirect if UR has trailing slash):
DirectorySlash Off
RewriteEngine On
RewriteBase /
# get rid of trailing slash
RewriteRule ^(.*)/$ $1 [R=301,L]
But you may have that extra slash added by some other rule in your .htaccess. If above will not help and you have some rewrite rules already in place -- please post them in your question.

Categories