Need help writing htaccess rule - .htaccess

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]

Related

url rewriting with QUERY_STRING

I would like to rewrite http://example.com/test/index.php?id=123
to http://example.com/test/item/123/ and http://example.com/test/index.php?id=123 redirect to the human friendly url.
I don't know what is wrong in my htaccess :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test/
RewriteCond %{QUERY_STRING} ^id=([a-z0-9]+)$ [NC]
RewriteRule ^/item/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^item/([a-z0-9]+)/$ index.php?id=$1 [L]
You can use the following RewriteRule in your /test/.htaccess file
Options +FollowSymLinks -MultiViews
RewriteEngine on
#redirect old url to the new one
RewriteCond %{THE_REQUEST} /test/index\.php\?id=([^\s]+) [NC]
RewriteRule ^ /test/item/%1? [L,R=301]
#internally map new url to the old one
RewriteRule ^item/(.+)/?$ /test/index.php?id=$1 [L,NC]

change to https and redirect to subfolder rule in .htaccess

I am trying to do two things here:
redirect to a sub-folder
redirect http://www.something.com/some/page.html
or
https://www.something.com/some/page.html
to
https://www.something.com/subfolder/some/page.html
redirect http to https
redirect
http://www.something.com/subfolder/some/page.html
to
https://www.something.com/subfolder/some/page.html
And I want to do both of them in the same .htaccess file
I have been able to redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
And then I am trying to do both of them; i.e. http to https(only if http request comes) and redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
But it's not working.
What am I doing wrong here?
EDIT
When using #starkeen's solution; i.e.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ https://www.example.com/subfolder/$1 [R=301,NC,L]
I am expecting the following as result:
https://www.example.com/subfolder/brands/omega.html
when I give any of the following:
http://example.com/brands/omega.html OK
https://example.com/brands/omega.html OK
http://www.example.com/brands/omega.html OK
https://www.example.com/brands/omega.html OK
http://example.com/subfolder/brands/omega.html WRONG
http://www.example.com/subfolder/brands/omega.html WRONG
But the last two are redirecting to
https://www.example.com/subfolder/
Here is a rule to do both the tasks in a single rule:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(?:subfolder)?(/.*)?$ https://www.example.com/subfolder$1 [NE,L,R=302,NC]
Make sure to clear your browser cache before testing this rule.
Try :
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#--Http ==>https--#
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
#--exclude the destination to avoid redirect loop--#
RewriteCond %{REQUEST_URI} !^/subfolder
#--redirect /foo to /subfolder/foo--#
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC,L]
Clear your browser'cache before testing this redirect.

.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

Create SEO Friendly URLS doesn't work (.htaccess)

I created an htaccess i want to rename all extension .php files to .html
i put
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
and it work fine.
Now i want to rename nettoyage-detail-bureaux.php?id=21 to nettoyage-detail-bureaux-21.html
so i add
RewriteRule ^nettoyage-detail-bureaux-([0-9]+)\.html$ nettoyage-detail-bureaux.html?id=$1 [L]
it doesn't work.
Any idea ?
I think you might have a typo at the end where you have .html?id=$1 it should be .php?id=$1
So try this.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^nettoyage-detail-bureaux-([0-9]+)\.html$ nettoyage-detail-bureaux.php?id=$1 [L]
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]

htaccess adding query string to 301 redirect

Can sombody help me out with this, im trying to re direct a page using htaccess file but it keeps adding ?c=oldpage on to the end of the new url, example:
http://www.mydomain.co.uk/newpage.html?c=oldpage
i have tried some of the solutions posted here but no luck, here is my .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteEngine on
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
Redirect 301 /oldpage.html http://www.mydomain.co.uk/newpage.html
ErrorDocument 404 /404.php
Thanks for any help.
This is mod_alias (the Redirect directive) and mod_rewrite not playing nicely with each other. Because both modules apply their directives on the same URI in the URL-file mapping pipeline, they don't know to ignore each other since neither directive knows what the other module is doing. Since you're targets overlap, both modules are applying their directives on the same URI and you get a mish-mashed result.
You need to stick with mod_rewrite in this case and move the redirect above the internal rewrites:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
# redirects
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^oldpage.html$ http://www.mydomain.co.uk/newpage.html [R=301,L]
# internal rewrites
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\/" [R=301,L]
RewriteOptions inherit
to a folder, or:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\.html$" [R=301,L]
RewriteOptions inherit
Don't know much about it, but it's what i use, hope it helps.

Resources