htaccess rewrite doesn't work properly on localhost - .htaccess

I'm restructuring a site now, and I'm testing it on localhost (xampp).
In the root directory there were a couple of .htm files, which were converted to .php.
However I'd like to keep the .htm extension for SEO reasons.
So I created a .htaccess file with this content:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)\.php$ $1.htm [L]
But when I try to access a page like this to test rewrite:
http://localhost/sitename/anything.php
I always get to
http://localhost/C:/xampp/htdocs/sitename/anything.htm
which obviously produces a 403 error.
I have tried to change .htaccess options, but no success.
Rewrite module is enabled in Apache, so it is not the case.
Am I missing something?

Try adding a RewriteBase directive i.e.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php$ $1.htm [L]

Try to redirect using the requested URI:
RewriteEngine On
RewriteRule ^(.*)\.php$ %{REQUEST_URI}.htm [R=301,L]
I tested it and it added ".htm" to all my php URIs test.php -> test.php.htm
No need for backreferences here.

Related

htaccess ignore subdirectory

I have problem with my .htaccess file (or wamp setup).
Here is my .htaccess:
RewriteEngine On
# Options +FollowSymLinks
RewriteBase /
RewriteRule ^directory/?$ directory.php [L,NC]
RewriteRule ^directory/subdirectory/?$ subdirectory.php [L,NC]
When I go to my_web/directory/ - everything is OK, but when I go to my_web/directory/subdirectory/ then show directory.php (url in browser is directory/subdirectory/).
When I write url without rule (example /test/) and I have test.php then automatically show test.php. Why?
Thank you.

htaccess rewrite folder to root

I want to redirect (joomla - new setup, old setup has a dual language, this one only with one, and /index.php/en/[content] links not work, aliases is still same) /index.php/en/[content] to /index.php/[content]
With .htaccess rewrite, I tried few options without success.
this .htacess config redirect /index.php/en/(anypage) to /index.php/(anypage)
Options +FollowSymlinks
RewriteEngine On
RewriteRule index.php/en/(.*) index.php/$1 [R=302,L]
Based on Froggiz answer(don't worked for me):
I modified the code, and it works correctly!
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^index.php/en/(.*)$ /index.php/$1 [R=301,NC,L]

Need change inside htaccess file

i have following htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule !^/?(maintenance\.html|.*\.css|.*\.js)$ http://www.example.com/maintenance.html [R=302,L]
RewriteRule ^([A-Za-z0-9]+)$ $1.php
RewriteRule ^home\.php$ index.php
RewriteRule ^home$ index.php
as you can see all, no matter which page you visit on the website it will be redirected to maintenance.html. I also need a small change that will exclude one more location from this. Since I am using Magento, I need to exclude admin area from this redirect so I think these are the urls that I need
http://www.example.com/index.php/admin_852_in
http://www.example.com/skin/
http://www.example.com/js/
http://www.example.com/media/
What rule should I add for these two urls too?
thanks!
You can tweak your exclusions like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule !^/?(maintenance\.html$|index\.php/admin|css/|js/|.*\.(css|js)$) http://www.example.com/maintenance.html [R=302,L,NC]

301 redirect a folder but exclude child folder

I need to redirect a directory "mysite.com/blog" to a new site. However, I do NOT want to redirect a child folder "mysite.com/blog/wp-admin".
Here is my htaccess code which effectively redirects the entire /blog folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
</IfModule>
I have tried the following to exclude /wp-admin, but that didn't work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/blog/wp-admin(.*)
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
Any ideas? Thank you!
UPDATE:
I tried removing the (.*) from both lines, as directed, and its still redirecting to the new site. BUT, the URL is different. Here's the URL once it redirects:
http://www.newsite.com/newblog/?redirect_to=http%3A%2F%2Foldsite.com%2Fblog%2Fwp-admin%2F
Can you tell from this URL what's going wrong? I'm not good at regex, but it looks like it's a formatting issue.

.htaccess php to html only in root

I been using .htaccess to change php to html and it works great, but I hit a snag when I forgot that some of my static html pages in subfolders don't appear in the web browser.
Is there a way I can make it so I can only have the root folder uses the .htaccess rule and not the subfolders?
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
For this case define your .htaccess rule like this:
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteRule ^(?![^/]+/)(.+)\.html$ $1.php [L]
Negative lookahead will prevent sub directory rule implementation.
You can specific a rule to work only on current dir. Like:
/your/dir/here/.htaccess
You .htaccess will be:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\/here(.*)\.html$ $2.php [L]
$2 mean get second group.
Edit: alternativaly, you can make a .htaccess on subfolder and disable RewriteEngine.

Resources