I'm using https://example.com/post?id=post_name in my custom blog built with PHP and I want to use https://example.com/post_name instead.
But I also use http://example.com/deal?id=deal_name for displaying online shopping deals and I also want to change that to http://example.com/deal_name.
How do I do it? My current .htaccess file is this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Options -Indexes
You can't do exactly what you want because you want to rewrite URLs to two different pages so you need to differentiate one type of URL from the other.
For example you could use https://example.com/posts/post_name for the post URLs and http://example.com/deals/deal_name for deals, you can do this adding the following rules before the current rules of your .htaccess file (just after RewriteEngine On):
RewriteRule ^posts/(.+)$ /post?id=$1 [NC]
RewriteRule ^deals/(.+)$ /deal?id=$1 [NC]
Another option is if post_name and deal_name have always a token that differentiate both types, for example if post_name starts always with p- and deal_name starst always with d- you could use:
RewriteRule ^p-(.+)$ /post?id=p-$1 [NC]
RewriteRule ^d-(.+)$ /deal?id=d-$1 [NC]
Related
I am using some mod-rewrite code in my sites .htaccess file to redirect users with browser languages set to various languages to different versions of my site. This is working well - but... I need a way to be able to override these rewrite rules when a user wants to view the main English version instead of the version they would get automatically redirected to based on the language setting of their browser.
So for example, user A has their browser language set to Korean, they visit the example.com site - the rewrite rules reads they are set to Korean and moves them to example.co.kr - on example.co.kr I have a button which says - Show me the English version of this website (which links to example.com) - this then links back to example.com... but currently then redirects back to example.co.kr as the rewrite script refires...
How can I code the rewrite script to allow for an overwrite rule to force it to stay on the example.com site when I want it too...?
rewrite code I have is:
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP:Accept-Language} ^en [NC]
#RewriteRule ^$ https://example.com/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ https://example.com/ [QSA,NC,L]
Any ideas?
Cheers,
You need to modify your rules to look for a special query parameter e.g. notKO like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ https://example.com/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ / [NC,L]
Now you need to start sending ?notKO query parameter on your show me abc version of this website button click.
Clear your browser before you test this change.
I have the following in my htaccess file:
# drop tags
#RewriteCond %{THE_REQUEST} (.*)designs/(.*)/?tag=shirts [NC]
#RewriteRule .* /designs/%2/ [R=301,L]
RewriteCond %{THE_REQUEST} (.*)designs/([^?]+)\?tag=[^&]* [NC]
RewriteRule .* /designs/%2? [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^designs/.*$ /$0? [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^mfp= [NC]
RewriteRule ^designs/ %{REQUEST_URI}? [L,NC,R=301,NE]
My theme supports a blog but after enabling it (it defaults to example.com/blog/), when I click on the blog link in my menu, it takes me to a page that says "There is no product that matches the search criteria". When I remove the htaccess rules listed above, the blog page (which contains the article listings) works fine so it is definitely that.
How can I exclude the word "blog" from the htaccess rules so this issue disappears?
I managed to fix it after a couple hours of tinkering. The solution was:
# external redirect from actual URL to pretty one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_URI} !^/blog($|/)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/designs/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
This solution also handles /blog (without the trailing /).
Hope that helps someone in the future.
I want to redirect example.com to example.au,but at the same time i want to exclude example.com/admin from redirection. Is it possible if yes please provide any rewrite rules.Any help would be appreciated.
Code:
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
#RewriteRule .* index.php [L]
RewriteRule ^(.*)$ www.example.com.au/$1 [R=301,L]
RewriteRule !^(admin/.*)$ www.example.com.au/$1 [R=301,L]
You can use following .htaccess redirection code for access admin
GeoIPEnable On
ReWriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} IN$ //IN is the country code where you want to redirect
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !index.php [NC] //admin keywords
RewriteRule ^ http://www.domain.in [R=301,L] //redirection url
In the above example admin and index.php are the keywords for accessing admin
RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !index.php [NC]
It means redirect to specific url in INDIA,only when the current url doesn't have the above keywords(all admin modules only have these keywords).
It would be help you. I'm also using the above same code for my store
All the best
EDIT : You can refer the following threads too for getting some more details
Some admin modules redirect to frontend
Magento redirection with htaccess based on country wise
I am trying to setup a codeigniter app to force HTTPS across all pages except one. However, I cannot get the rules to only redirect if the user is not on the page in question.
The page that should be excluded has the following URL's:
http://mydomain.com/kpi/reports/67
http://mydomain.com/kpi/reports/67/overview/2013-02-01/2013-02-28
http://mydomain.com/index.php?/kpi/reports/67
http://mydomain.com/index.php?/kpi/reports/67/overview/2013-02-01/2013-02-28
The number 67 and the dates can all change in the URL's above hence the user of regular expressions below.
I have tested the regular expressions and they seem to match the URL's fine. However, the htaccess just seems to redirect it to https:// anyway.
My .htaccess file is as follows...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Disallow access to system dir
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Disallow access to application dir
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help would be greatly appreciated!
Maybe this will do what you need:
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
Replace all lines between the comments.
For a multi-langue site I have used the following code to automatically redirect people to the right page. This works great, but does not include stylesheets and other files.
The following code in .htaccess turns www.site.com to www.site.com/fr if a visitor is from France. But it also redirects stylesheet and other files.
RewriteCond %{HTTP:Accept-Language} ^(nl.*) [NC]
RewriteCond %{REQUEST_URI} !(^/nl/.*) [NC]
RewriteRule ^(.*)$ /nl/$1 [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^(fr.*) [NC]
RewriteCond %{REQUEST_URI} !(^/fr/.*) [NC]
RewriteRule ^(.*)$ /fr/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(^/.*) [NC]
RewriteRule ^(.*)$ /$1 [L,R=301]
www.site.com/css/style.css turns into www.site.com/fr/css/style.css which doesn’t exist
www.site.com/js/file.js turns into www.site.com/fr/js/file.js which doesn’t exist
Is there a way to create an auto redirect, but exclude whole subdirectories? For example, exclude /css/. and /js/.
You can use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
so that requests to files and directories that exist will not be rewritten