Hello! I've run into a problem with my .htaccess that I needed some suggestions with. I'm trying to do a few things: 1.) Redirect www.mydomain.com to mydomain.com 2.) Rewrite mydomain.com/page to mydomain.com/page/ 3.) Have mydomain.com/page/ rewrite to index.php?page=$1 4.) Make it so that if anyone tries to go to index.php or a page ending in .php, it either redirects it or renames it - something to hide the fact that it's coded in php. Here's what I've got: RewriteEngine on RewriteCond %{http_host} ^www\.mydomain\.com [nc] RewriteRule ^(.*)$ http://mydomain.com/$1 [r=301,nc] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^([a-zA-Z0-9_-]+)$ /$1/ [R] RewriteRule ^(.*)/$ index.php?page=$1 [L] Code (markup): I'm not sure of how the syntax is supposed to be, but what I've got working is redirecting to a "www"-less site, and when mydomain.com/page/ has a forward slash at the end, then it points it to index.php?page=$1 just fine, but if it doesn't have that forward slash, I get a 404. Also, it still works if I go to index.php, and I want to hide that or redirect that. Any suggestions?
The below set of rewrites should do what you want unless there is a directory with the ame name as the php file. For example, you have foo.php and foo/ in you document root. When you request http://www.example.com/foo and you get a 404 and you don't get redirected to the trailing slash version. You will also not get redirected if you don't have a PHP file called foo.php. If you drop the two RewriteCond lines before the RewriteRule you might have some more luck with that one. You could also try adding a question mark after the slash in the following rewrite rule or dropping the slash and the dollar sign. Like this: RewriteRule ^(.*) index.php?page=$1 [L] Code (markup): or this: RewriteRule ^(.*)/[B]?[/B]$ index.php?page=$1 [L] Code (markup): What this will do is allow your pages to work with or without the trailing slash. The downside to this is that you will have the same content available on two different URLs which can make Google slightly unhappy. On the other hand, if it gets your site working then it will make your users happy... and they are more important than Google. In any case, Google should be smart enough to realise that the same URL with a trailing slash is essentially the same URL.
Will this protect me if a user tries to find out what code I'm using by checking to see if index.php exists? I want to hide that, similar to how Google does it I believe. And what might be a possible fix to make Google "happy"? I wouldn't want to have duplicate entries in Google search results when Googlebot scans my page. And what exactly do those Rewrite Conditions mean that I posted before? Is there anything I need to add just to be safe? I apologize for bombarding you with questions, but I'd like to get the most out of my website and its security while making it friendly to its users and search engines.
No. If they request index.php?page=foo then they will get the same page as if they requested /foo. They don't really gain very much information by knowing that you are running PHP. It's basically the default guess if they don't know anyway. I wouldn't worry about it but if you are still worried you could add a rule that looked for "index.php" and changed it to something else. If you don't link to it, Google won't find it. As for the RewriteCond lines, the first one: RewriteCond %{REQUEST_FILENAME} !-d Code (markup): means "If the filename requested is not a directory on my disk" and the second one: RewriteCond %{REQUEST_FILENAME}\.php -f Code (markup): means "If the filename requested with .php on the end is a file on my disk" RewriteCond lines have an implicit AND between them so the requested filename has to be a PHP file and not be a directory for the RewriteRule below them to run.
How can I get it so that Google won't duplicate my entries in a search result for the same page? I'd like it to have a trailing slash and have that page show up in the results rather than have two separate results but the same page just because there is or isn't a trailing slash. How can I counter that? 301 Redirect? How would I go about doing that?
I found a solution: Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([a-z0-9_-]+)$ /$1/ [NC,R=301,L] RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([a-z0-9_-]+)/$ /index.php?page=$1 [NC,QSA,L] Code (markup): The conditions for determining if a .php file with the same name had to be removed because they were making the Rewrite Rules not work, though. Not sure what that will do if I have other .php files. The conditions that had to be removed were: RewriteCond %{DOCUMENT_ROOT}/$1.php -f Code (markup): They came right after the two script_filename conditions and were prohibiting the rewrites from working.
I know this is an old thread, but I've come back to this and am having some problems. When I use that rewrite and I try to access a folder on the site, it just takes me to the "index.php?page=$1" page and not the actual folder. Is there a way to get the rewrite to work so that I can access folders and have the other files rewrite if they're not actual folders?