How to remove the first Slash after the root directory - .htaccess

When I visit
myexamplesite.com?var/othervar
i get redirected to
myexamplesite.com/?var/othervar
but i want the URL to stay like this: myexamplesite.com?var/othervar
Is it possible to remove the first / after the root url using mod_rewrite?

No, it is impossible with htaccess and with everything else.
It depends on the browser you are using, that's why the root slash is added automatically.

Related

Adding a subdirectory to an invisible .htaccess URL rewrite

I wanted to add a subdirectory to my url so it would become easier to read:
Example of what i'd like:
localhost/testwebsite/users.php?firstname=john
should become
localhost/testwebsite/users/john
I tried using the .htaccess mod_rewrite rules and came up with this:
RewriteEngine On
RewriteBase /testwebsite/
RewriteRule ^users/(.*)$ users.php?firstname=$1
What happens why I use that code: it redirects the page successfully, it shows the html of the correct user and it processes the argument correctly. However, all stylesheets, images, scripts, anything with a relative path, could not be found and respond with a 404 message, because of the extra subdirectory added in the new url, I reckon.
Am I doing something wrong? Is there another technique I should be using? Or should I simply make all paths in my project absolute with regards to the root?
You're doing it right. The browser doesn't know that the actual path of the file is different. Use absolute paths or make paths relative to the easier to read URL.

Htaccess - Disallow going backwards?

is there a way to disallow to go backwards to another directory?
E.g.: The URL is http://www.example.com/subdirectory/
If there is a HTML file which links to the the URL "/file.txt" it should not leave the subdirectory. The slash has to be ignored. Are there some Rewrite Rules in htaccess to do this? The subdirectory should be handled like a root directory.
There is a similar question here. I believe it is what you are looking for:
.htaccess. deny root, allow specific subfolder. Possible?

How to change base_url in drupal 7, fail to find answer online

I am trying to change base_url on my drupal 7 website, but failed to do it and google a lot, still could not solve it .
I am running localhost/drupal "on my local server. but I want to run it like
" localhost ". ( sorry I have to get rid of http://, otherwise it doesnot let me post)
How to get rid of folder in the url. I know I need to change $base_url on sites/default/setting.php to
$base_url = 'localhost'; // NO trailing slash! ( sorry I have to get rid of http://, otherwise it doesnot let me post)
And in the .htaccess file I am so confused what should I change. People online have their own solution, some said they work , some could not . Could someone give me some suggesttion ?
I assume this has to do with where you have placed your files.
In your file system, remove the 'drupal' directory and place all the files within that at /var/www/html ... instead of /var/www/html/drupal, if that's how it is currently set up.
I feel your pain; I've had this trouble myself before and I believe I had to alter both the settings.php and the htaccess (but try one at a time to begin with). I'm fairly sure that the section below in your .htaccess is what needs changing (just remove the hash from in front of the RewriteBase line). If you're unsure of what you're doing, make a copy of your .htaccess as htaccess.txt and then you can always switch back over if things get nasty.
# Modify the RewriteBase if you are using Drupal in a subdirectory or in a
# VirtualDocumentRoot and the rewrite rules are not working properly.
# For example if your site is at http://example.com/drupal uncomment and
# modify the following line:
# RewriteBase /drupal
I just realised that what you need is a re-write rule in an .htaccess file within your root folder (not the one that's inside the drupal folder). First, undo any edits you've already made, then create an .htaccess file in the same folder that contains your drupal folder and in it, add:
RewriteEngine On
RewriteRule ^$ /drupal [L]
That should send any traffic from the root into the drupal file structure. If you get issues with strange css, or urls that include the /drupal path, you'll need to revisit the other options in the /drupal/.htaccess file and your settings.php file in order to get it all working correctly.
However, going down this route, you may as well just copy all the files and folders into the root directory anyway (as the first answer suggested), assuming you're not going to be running multiple sites. If you do want to be testing multiple sites, you can just change the RewriteRul above to whatever site directory you're wanting to test in future.

prevent htaccess from loading rewritten page in new folder

Following up on a previous question I asked (htaccess load page B instead of page A without redirecting) I've run into this issue:
user goes to .com/A , then .com/B is loaded without changing the URL. this is fine.
But, the Rewrite rule also allows for a trailing slash and anything that would follow:
RewriteRule ^(a|b|c)[\/.*] /d [L]
The problem is, when .com/A/ is loaded, all relative hrefs and links point to that 'fake' /A/ folder.
What should I do to prevent this, and have .com/A/ (with the trailing slash) still act as if it is part of the root directory that .com/index.php would use?
Thanks!
One thing to note with rewrites. I would use absolute paths or / in front of the path, for CSS and JS files. relative paths will mess you up every time.

Foward directory to domain with parameters stripped for dir only not site wide

I'm permanently forwarding a directory to its own domain, i had a wordpress setup in the directory at one time which google indexed... I've setup the redirect in my .htaccess however permalink structure is appended to the forwarded domain. So im taking everything from /sample forwarding to www.sample.com but im wanting to strip the unused variables from the url such as www.sample.com/?p=6, etc... However i need this done only for files in the /sample directory as i still have wordpress installs that need to remain intact. Also how can i not have a double forward slash after the redirect. I have:
Redirect permanent /sample http://www.sample.com
But it outputs http://www.sample.com//
Try this instead:
Redirect 301 /sample/ http://www.sample.com/?
The ? should strip query string (?p=6 etc)
The same but using mod_rewrite:
RewriteRule ^(sample(/|/.*)?)$ http://www.sample.com/$1? [NC,R=301,L]

Resources