Rewrite URL with .htaccess - .htaccess

I've this url: http://www.test.com/page.php?k=m1ns
and I want this one: http://www.test.com/r/m1ns
My .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^k/([^/\.]+)/?$ page.php?k=$1 [L]
# force www. in all requests
RewriteCond %{HTTP_HOST} ^test\.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301]
# enable hiding php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But it doesn't work. Only the non-www -> www and hiding php rules works.
If I put http://www.test.com/page.php?k=m1ns does not rewrite.
Anyone knows why?
Thanks.

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^r/([^/]*)$ /page.php?k=$1 [L]
On your top page.php
if (strstr($_SERVER['REQUEST_URI'], '/page.php?k=' . $var . '')) {
header("HTTP/1.1 301 Moved Permanently");
header("location:http://www.test.com/r/" . $var );
exit();
}

Try this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^r/(.*)/ /page.php?k=$1 [L]
It should work regardless of whether or not www is entered.

Related

.htaccess redirect not properly

I would like to redirect my pages in the following ways:
www.example.com/index.cfm?locale=it -> www.example.com/it
and I would like to access the same page when I type:
www.example.com/it
I wrote an htaccess file but it causes a redirect loop. Here is the file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#REWRITE RULES
#---------------------
RewriteCond %{QUERY_STRING} ^locale=(it|en|fr|de|es)$
RewriteRule ^index\.cfm$ %1? [R=302,L]
RewriteCond %{REQUEST_URI} !^index\.cfm
RewriteRule ^(it|en|fr|de|es)/?$ index.cfm?locale=$1 [L]
</IfModule>
Could you help me to resolve it?
Thanks!!!
You can try it this way.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
Rewrite ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ /index\.cfm\?locale=(it|en|fr|de|es)
RewriteRule ^ %1? [R=302,L]
RewriteRule ^(it|en|fr|de|es)/?$ index.cfm?locale=$1 [L]
Try this:
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/it[nc]
RewriteRule ^(.*)$ http://www.example.com/it/$1 [r=301,nc]
//301 Redirect Old File
Redirect 301 www.example.com/index.php?locale=it www.example.com/it
//301 Redirect Entire Directory
RedirectMatch 301 www.example.com/index.php?locale=it(.*) www.example.com/it/$1

Need help writing htaccess rule

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]

How can I exclude a page from .htaccess rules?

This is my .htaccess file:
Options -Indexes
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|jscript|user_guide|login|en|favicon.ico|uploads|script.php)
RewriteRule ^(.*)$ index.php/$1 [L]
And I want to be able to type this adress directly:
mysite.com/system/application/views/req-php.php?_INPUT&f=xxx
You can add another negative RewriteCond to avoid rewriting for this new URL:
Options -Indexes
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/system/application/views/req-php\.php\?_INPUT&f= [NC]
RewriteCond $1 !^(index\.php|css|images|jscript|user_guide|login|en|favicon.ico|uploads|script.php)
RewriteRule ^(.*)$ index.php/$1 [L]

.htaccess redirect subdirectory to index.php

i want to rewrite url like:
http://domain.com/index.php/subdirectory/subdirectory/
to:
http://domain.com/index.php
My rewrite rule look like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)
RewriteRule ^(.*)$ index.php$ [L]
thanks,
The RewriteCond
RewriteCond %{REQUEST_URI} !(.*)
will always been false, you don't need it. Just use :
RewriteEngine On
RewriteRule ^(.*)$ index.php [L]

htaccess add index.htm at end of folder

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]

Resources