I'm new htaccess. So i need to rewrite .php to .html in url.
my files located in
http://www.domain.us/sub1/sub2/sub2/index.php
I need to rewrite as
http://www.domain.us/sub1/sub2/sub2/index.html.
I tried something
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} site
RewriteRule ^(.*)\.php$ http://www.domain.us/sub1/sub2/sub3/$1.html [R,L]
But It shows 404 error.
Thanks.
Put this code in your DOCUMENT_ROOT's .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/*sub1/sub2/sub2/.*\.php [NC]
RewriteRule ^(.*)\.php$ /$1.html [NC,NE,R=301,L]
Related
Currently my localhost URL is like localhost/apnaujjain/page.php?page_id=1&action=contacts
Now I want to write a rule so that when I go to above url it just rewrite the url like localhost/apnaujjain/contacts.html
See it should convert .php into .html without affect page content.
Until now I've tried
Options +FollowSymLinks -MultiViews
RewriteEngine on
#RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.php?page_id=1$&action=2$
RewriteRule ^(.*)\.php $2.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
But it's not working.
You can use this code in /apnaujjain/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/
# redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to
# localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^.]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
I need a way to add index.htm at end of url.
For example:
www.example.com/folder1/folder2 AND
www.example.com/folder1/folder2/
must be redirected internally to www.example.com/folder1/folder2/index.htm (this must apply for any number of folders/subfolders)
My htaccess (at root) so far looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /
#
# some 301 REDIRECTS here
#
# rewrite non-www into www
# after all redirection rule AND before any rewrite rule
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# rewrite rules below here
#
Any way to fix this?
Thank you.
EDIT:
- All urls come from rewrite rules - there are no actual folders in site.
- Typing www.example.com/folder1/folder2 in my browser, only get a 404 page not found. I need it to redirect to www.example.com/folder1/folder2/index.htm (there is a rule creating this url)
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.htm$ [NC]
RewriteRule ^(.*?)/?$ /$1/index.htm [L,R=301]
Do you mean you want to REDIRECT /folder1 and /folder1/ into /folder1/index.htm, and /folder1/folder2 and /folder1/folder2/ to /folder1/folder2/index.htm, and /folder1/folder2/folder3 and /folder1/folder2/folder3/ to /folder1/folder2/folder3/index.htm
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/%3/index.htm [R]
I have a nice rule for rewriting the url to /meetings, but the rule doesn't rewrite the url properly when visited from a subdirectory.
EDIT: My .htaccess file is in the subdirectory /blog
When I use this rule:...
RewriteRule ^meetings$ /?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): 404 error
When I use this rule:...
RewriteRule ^meetings$ http://blog.example.com/?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): blog.example.com/?page_id=2809
Is there a way to get the rule to work consistently for the subdomain and the subdirectory?
You may try this in one .htaccess file in /blog directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /blog
RewriteCond %{HTTP_HOST} blog\.example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /?page_id=2809 [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /blog/?page_id=2809 [L,NC]
For permanent redirection, replace [L,NC] with [R=301,L,NC].
I have the following .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
The problem is that all subdomains get redirected to www.mysite.co/subdomain
How can I aviod this?
Your rewrite rule logic in plain speak is doing the following:
For ANY HOST other thant www.mysite.co (including foo.mysite.com, blog.mysite.com, etc..)
Permanent Redirect (301) to http://www.mysite.co/
This is the last rule to check in the .htaccess file, so bail out
To not redirect your own subdomains, the easiest and clearest way is to handle it explicitly with more rewrite conditions (see example below). For more kung fu htaccess fun try this: .htaccess Wildcard Subdomains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^blog.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^foo.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
You need to run your index.php file
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
I have the rule below which works perfectly, however My oldsite urls have .html and me newsite doesn't. Is it possible to strip the .html before redirecting? e.g.
www.oldsite.com/mypage.html
gets redirected to
www.newsite.com/subdir/mypage/
e.g.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.oldsite.com$ [NC]
RewriteRule ^(.*)$ http://www.newsite.co.uk/subdir/$1 [R=301,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.oldsite.com$ [NC]
RewriteRule ^(.*)\.html$ http://www.newsite.co.uk/subdir/$1 [R=301,L]
so www.oldsite.com/mypage.html will be redirected to www.newsite.com/subdir/mypage