prevent htaccess from loading rewritten page in new folder - .htaccess

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.

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 for local testing and live server

I'm close, to my final solution I think.
The .htaccess looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /myproject/development/
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]
RewriteRule ^((?!index\.php)[^/]+)/([A-Za-z0-9]+)/([0-9]+)/([0-9]+)/?$ index.php?page=$1&keyword=$2&zip=$3&range=$4 [L,R]
I don't need the RewriteBase for the 1st rule(was a little surprised about that) but I need it if I add the 2nd rule and open this URL:
//localhost/myproject/development/somepage/test/13245/50
Otherwise the page will be opened but of course without the stylesheets and javaScripts can be found then.
1.) Target: I want to use the 2nd rewriteRule without changing or adding a rewriteBase. What do I need to change in the .htaccess so I can keep testing my project without a rewriteBase.
Why: As I asked before I want to test my project locally and on the live-server without changing too much on the project configuration.
2.) The [R] Flag If I request
//localhost/myproject/development/somepage/test/53229/2000
Of course in the adressline then we have this URL
//localhost/myproject/development/index.php?page=somepage&keyword=test&zip=12345&range=2000
To avoid this behaviour I simply should remove the R-Flag. But then the CSS and JS can't be found anymore. Also Here I'm looking for a solution without rewritebase, basepath, virtual host, etc. if possible.
Here is where I started:
Rewrite rules for localhost AND live envoirement
By the looks, you don't want the R flag on the 2nd RewriteRule. That defeats the object of your "pretty" URLs.
But then the CSS and JS can't be found anymore.
Because you are using relative paths to your CSS and JS files. Either change your paths to root-relative (starting with a slash), or use the base element in the head section of your pages, to indicate the URL that all relative URLs are relative to:
<base href="http://www.example.com/page.html">
More Information:
https://developer.mozilla.org/en/docs/Web/HTML/Element/base
I don't need the RewriteBase for the 1st rule(was a little surprised about that)
You don't need the RewriteBase for the 1st rule (an internal rewrite) because the directory-prefix (the filesystem path that lead to this .htaccess file) is automatically added back on relative path substitutions.
However, for external redirects (ie. R flag), the directory-prefix does not make sense, so you either need to specify a root-relative (starting with a slash) or absolute URL. Or specify the appropriate RewriteBase directive, which overrides what URL-path will be added for relative substitutions (that's all it does).

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.

How to remove the first Slash after the root directory

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.

.htaccess rewriting - URLs with multi level directories

I have a page deals.php which is placed at root directory that is being accessed by number of internal URLs with multi directory levels. for example this page would be accessed by both following urls.
http://domain/flights/asia/bangkok
http://domain/flights/bangkok
I am using this code in .htaccess to redirect to deals.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^flights/asia/([^/]+)/?$ deals.php?country=$1 [NC]
RewriteRule ^flights/([^/]+)/?$ deals.php?country=$1 [NC]
Now the problem here coming is that on deals.php all the images, script and style sheet files are not opening properly. If I try to fix it against one URL by placing ../../ in addresses of all images, script and css, it dont work for other URL.
How to solve this problem? what are the options?
Easy: DO NOT use ../ in links to any resources (images/css/js/etc) -- always use URL that is relative to the WEBSITE ROOT -- that is a "requirement" when you dealing with nice/not-real/rewritten URLs as such URL rarely points to the physical file location.
Lets assume you have a logo that is located at http://www.example.com/images/logo.png.
Now, instead of ../images/logo.png and/or ../../images/logo.png ALWAYS use /images/logo.png.
i think what you want is placing an baseurl in html page, so all the relative path will related to the baseurl, not the current URL in the navigator bar

Resources