mod_rewrite: argument is visible if URL refers to a folder - .htaccess

I'm using the following RewriteRule in my .htaccess, which is working fine.
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
However, there is one thing that is troubling me:
I have a subfolder called "folder"
I request the URL http://www.example.com/folder
The URL changes to http://www.example.com/folder/?q=folder
Any other URL works as expected, including http:// www.example.com/folder/
It doesn't make a difference as far as the GET variable is concerned, of course.
But it would be nicer if the argument would not become visible in the browser. I tried adding a slash before index.php, but that resulted in an Internal Server Error.
Any help would be appreciated!

Add this before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Related

Why is htaccess ignoring my first rewrite rule?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^myfile\.php$ redirector\.php?og_path=new_file_1[L,NC,QSA]
RewriteRule ^myfile\.php$ redirector\.php?og_path=new_file_2[L,NC,QSA]
I have my htaccess set up with a simple rewrite rule similar to above. When I go to www.mysite.com/myfile.php the first redirector will be ignored and instead it will automatically go to the second (new_file_2). Every other rewrite rule works as expected.
Curiously though, it works fine on my localhost, but for some reason on my server it give me the error.

htaccess mod rewrite for phpmyadmin

I have been searching with no success on getting my mod rewrite rule to work. I am creating a site like most now that use a rewrite rule to point back to a specific php file that will display content based on the url. The issue i am having, is trying to get the rewrite rule NOT to apply if the user went to "something.com/phpmyadmin" I know it has been asked before, and i have read those posts, but i still cant seem to get mine to work, here is the rewrite scripts i have, and it is still sending me to the /index.php instead of /phpmyadmin/
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/phpmyadmin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
If i go to /phpmyadmin/index.php it works fine, but i am really bothered why i cant get the directory part to work.
You should use THE_REQUEST variable:
RewriteEngine on
RewriteCond %{THE_REQUEST} !/phpmyadmin [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

.htaccess. Redirect requests to ~username folder

Situation:
I'm moving a website from a production environment to a test environment.
The test environment url is similar to http://192.168.1.100/~username/
There are thousands of files which use the following within the html
<img src='/images/image.jpg' />
Since the request is going to root http://192.168.1.100/ the files are 404.
Rather than finding and replacing all of html I'd assume that there is an easy way to fix it with mod_rewrite via .htaccess.
I've tried using the following
RewriteCond %{REQUEST_URI} !^/~username/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~username/$1
But did not work as expected.
Thanks in advance.
UPDATE
The development environment resides within cpanel/whm. So when the username is removed from the requested url, it now belongs to the root users. So, my question now: How do I update the .htaccess file for the root user to mod_rewrite back to the ~username?
If you remove
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
it appears to work as expected, because any request to the right url will not be rewritten.
you might want to add [L] as a flag to signify it's the last rewrite rule, like so:
RewriteCond %{REQUEST_URI} !^/~username/
RewriteRule ^(.*)$ /~username/$1 [L]

Rewrite Urls without breaking real files

So i'm pretty terrible at RewriteRule and .htaccess, and I've been trying to create a RewriteRule which allows me to redirect any request that doesn't already exist in the directory through index.php.
It looks like this at the moment:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?name=$0 [L]
However once I started trying to run ajax requests from a redirected url it breaks and returns the whole redirected page instead of the return data of any script that is in the directory. I'm pretty confused as to how I can make this work. \
What I want to be able to do is to be able to rout every non-real request through index.php and every other request as normal.
You could sneak in another RewriteCond using a RegEx, in case you can find one (or more) for you normal requests. Something like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^*your* *RegEx* *for* *normal* *request*
RewriteRule ^(.*)$ index.php?name=$0 [L]
Your htaccess code is correct. "-f" checks for a valid file and "-d" for a valid directory.
My guess is that you issuing relative ajax requests from an url like /some-dir/another-path/ which is internally rewritten to index.php?name=/some-dir/another-path/. A relative ajax request to ie my-ajax-data.php will be made to /some-dir/another-path/my-ajax-data.php, and not /my-ajax-data.php as you might think. Try using absolute URLs in the ajax requests. Well, it's just a shot in the dark.

mod_rewrite redirect for non-existent URL's

I've got a an old website that I've taken over. New users essentially get a custom page created for them. It was done in a less than fantastic manner. Currently it actually generates a file named for the slug URL created and symbolically links it to a folder called "/main/". Obviously I want to change this. My plan was simply to have it redirect non-existant folders to "/main/" via .htaccess. Currently this is what my .htaccess looks like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|index\.htm)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /main/ [L]
However this generates a redirect loop. What am I missing?
Edit
On that note, I realized I should say I want it to maintain the path that's input. For example if I put http://www.mydomain.com/test_person it should maintain that address, but forward everything to the main folder if that makes sense.
You'll want your rule to not rewrite URLs already beginning with main in order to not have it loop, eg:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^main/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+/(.*)$ /main/$1 [L]
[L] doesn't really mean what you would think [L] should mean in the context of a .htaccess file.
You should be able to get the original url requested from the REQUEST_URI environment variable, but a common thing to do is to pass the slug to the target as a GET variable.
RewriteRule ^([^/]+)/(.*)$ /main/$2?user=$1 [QSA,L]
Or pass everything to a single script which interprets the URL and finds the correct content to return:
RewriteRule ^([^/]+)/(.*)$ /main/index.php?user=$1&path=$2 [QSA,L]

Resources