Htaccess clean urls code not working as I want - .htaccess

So here's what my htaccess clean url code look like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !^myvar=0
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^([^/]*)/?$ /$1.php?myvar=0 [QSA,L]
This htaccess code permits files on the root to work with .php, trailing slash, or nothing. So blah.com/blah.php works blah.com/blah works blah.com/blah/ works
However if there's a folder on my root, say the name is 'news', the file 'news' on the root cannot take on a clean url. So blah.com/news.php works but blah.com/news and blah.com/news/ both do not work.
For some reason, files inside a folder must have a file extension (.php), so blah.com/news/obama.php works blah.com/news/obama and blah.com/news/obama/ both do not work.
What's wrong with the .htaccess code and how can I correct it?
tl;dr Is there an htaccess code that can make it so .php works, trailing slash works, and no trailing slash works? I know absolutely nothing about htaccess.
Also: I posted this same question yesterday but was met with no reply. Not sure why. If there is something confusing, I'll try to clear it up. Thanks!

Maybe
RewriteBase /
RewriteRule ^(.*)(?=\.php/?)?$ /$1.php?myvar=0 [QSA,L]
could help. Your log files are really golden in this case.

Related

htaccess RewriteRule redirecting for URL's without trailing slashes

I have this snippet in my .htaccess file to defend against anyone trying to get into the app directory.
RewriteCond %{REQUEST_URI} ^/app.*$
RewriteRule ^(.*[^/])/?$ index.php?r=$1 [L,QSA]
And although it works when I go to http://domain/app/, if I make a request to http://domain/app, it redirects to http://domain/app/?r=app.
Does anyone know what needs to be changed to stop such redirection?
Try the DirectorySlash directive that can be use globally or per directory.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash
try using
[L]
instead of
[L,QSA]

.htaccess rewrite rule for /

I have a website where if I go to the URL http://mysite.com/community it shows page not found. But, the URL http://mysite.com/community/ correctly displays the page. How can I set up a rewrite for that "/" after community?
This is my present .htaccess:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^admin$ Admin/index.php?qstr=$1 [L]
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
These were the ones tried by me, but failed
First,
RewriteRule ^(.*)/community $1/community/ [L]
second,
RewriteRule /community /community/ [L]
All with different combinations of with and without [L].
From the Apache URL Rewrite Guide:
Trailing Slash Problem
Description:
Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. And because this file is a directory it complains. Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc.
Solution:
The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect!
So, to do this trick we write:
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R]
The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. But notice that this creates some processing overhead.
RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
Well, after trying out all the above solutions as well as some of my own, I finally solved this. I'm definitely sure that this is NOT a complete solution but it sure solved it for the time being.
Solution: Just created an empty directory named "community" in the root folder. That's it!
But I'm still on the lookout for the actual solution to this.

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]

How do I remove a trailing slash?

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.

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources