I am using below code:
#RewriteCond %{QUERY_STRING} !(^|&)sort_field=more&limit=&p=(&|$) [NC]
#RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
#RewriteRule ^ %{REQUEST_URI}?sort_field=more&limit=&p= [L,R=301,QSA]
But it add params on all pages. I need add params only to main domain. How can I do it?
First of all, these directives don't apply, because they have a hash # in front of them.
The query string (params) is added to all pages, because the pattern ^ (beginning of URL) matches all pages. If you want to match only a specific page, you must use an appropriate pattern, see Regular Expressions for details how to specify a pattern.
In your case, main domain (main page?), it would be just ^$ or ^index.html$ or similar, e.g.
RewriteRule ^$ %{REQUEST_URI}?sort_field=more&limit=&p= [L,R,QSA]
When everything works as it should, you may replace R with R=301. Never test with R=301.
Related
I have two requirements;
That, for example, /product/12345 is internally redirected to /product/product.php?product=12345.
That if the user tries to access /product/product.php in the URL bar, it is redirected to /product/ for tidiness.
Separate, they both work correctly, but together it results in an infinite loop - I know that I'm redirecting from /product/ to /product.php and back again, but the difference is internal vs external and I'm not sure how to distinguish between them.
RewriteEngine On
RewriteRule ^product/product.php /product/ [NC,R=307,END]
RewriteCond %{REQUEST_URI} !^/product/product.php [NC]
RewriteRule ^product/(.*) /product/product.php?product=$1 [NC]
There probably exist other solutions, but it works if you change two things:
Add a condition to the first RewriteRule that checks if the query string is empty, i.e. product/product.php without query string redirects to /product/.
Change (.*) in the second RewriteRule to (.+) or ([0-9]+) to only rewrite requests containing a product id (requests to /product/ are not rewritten).
RewriteEngine On
RewriteCond %{QUERY_STRING} ="" [NC]
RewriteRule ^product/product\.php$ /product/ [NC,R=307,END]
RewriteCond %{REQUEST_URI} !^/product/product\.php [NC]
RewriteRule ^product/(.+) /product/product.php?product=$1 [NC]
access /product/product.php in the URL bar, it is redirected to /product/ for tidiness
You might as well also redirect /product/product.php?product=12345 to the corresponding canonical URL (ie. /product/12345) - which you can do all in the same rule. If the product ID is numeric only then you should restrict your regex accordingly - this will also avoid the need for an additional condition.
For example:
# Canonical redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(?:product=(\d*))?$ [NC]
RewriteRule ^product/product\.php$ /product/%1 [NC,R=307,L]
# Rewrite requests from "pretty" URL to underlying filesystem path
RewriteRule ^product/(\d*) /product/product.php?product=$1 [L]
The condition that checks against the REDIRECT_STATUS environment variable is necessary to prevent a redirect loop in this instance since the query string is entirely optional.
By restricting the match to digits-only, we avoid the need for an additional condition on the internal rewrite, product.php won't match. If the product id can contain letters then restrict the pattern to avoid dots (.), eg. ([^./]*).
Only include a NC flag on the internal rewrite if this is strictly necessary, otherwise this potentially creates a duplicate content issue.
So I have this two sub sites that needs to be redirected into a different locale but has the same source, but only the first rewriterule takes effect for the both domain.
what should happen:
example-site1.com/register ----> https://register.index.com/en-us
example-site2.com/register ----> https://register.index.com/en-gb
what's happening for this code below:
example-site1.com ----> https://register.index.com/en-us
example-site2.com ----> https://register.index.com/en-us
###########################
RewriteCond %{HTTP_HOST} ^example-site1\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example-site1\.com
RewriteRule ^/(registration|registration/)$ https://register.index.com/en-us [L,R=301]
RewriteRule ^/(register|register/)$ https://register.index.com/en-us [L,R=301]
RewriteCond %{HTTP_HOST} ^example-site2\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example-site2\.com
RewriteRule ^/(registration|registration/)$ https://register.index.com/en-gb [L,R=301]
RewriteRule ^/(register|register/)$ https://register.index.com/en-gb [L,R=301]
RewriteCond(s) only affect the immediately following RewriteRule, not multiple rules - so your second RewriteRule above is completely independent of your host name checks above. You need to either combine those two rules into one (should be no problem in this instance, because the target is always the same URL, and you simply have four [actually rather two, see below] different alternatives to match here) - or you would have to repeat the RewriteConds again before the second RewriteRule. And the same for the second block.
(registration|registration/) is kinda redundant anyway - you don’t require a back reference to what was matched for your rewrite, so you could just add the slash after the keyword, making it optional - registration/?. So if you combine both cases into one pattern, ^(registration|register)/?$ should do the trick. (I removed the leading slash, with that it should actually not have worked at all to begin with - if you configure rewriting in .htaccess, that leading slash has been stripped off at that point already.)
I am trying to write a Rewriterule which takes a domain from a URL of the format
https://www.example.com/sample?TARGET=https%3A%2F%2Fwww.example.com%2Fexample%2Fhelp%3Fparam%3D1.
If the TARGET parameter is present I need to redirect the user to the value inside the TARGET query parameter. My rewrite rule is below:
RewriteCond %{QUERY_STRING} TARGET=([-a-zA-Z0-9_+]+)
RewriteRule ^(.*)$ %1? [R=302,L]
This does not work because of two problems:
%1? in the rewrite rule causes the rewrite to append the value of the TARGET query string to the existing domain.
The value of %1 only contains https rather than https%3A%2F%2Fwww.example.com%2Fexample%2Fhelp%3Fparam%3D1.
I understand that this might not be the best way to go ahead with this, and I am open to suggestions.
You can use this rule instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^TARGET=(.+)$ [NC]
RewriteRule ^ %1? [NE,R=302,L]
Important to use .+ in regex to be able to capture all characters of the URL specified in TARGET parameter.
This will redirect:
http://yourdomain.com/?TARGET=https://www.example.com/example/help?param=1 to
https://www.example.com/example/help?param=1
I need to cut some part of URI, but I still need to pass it internally.
Ex: www.hostname.com/category/N-abcdef
To: www.hostname.com/category
But I need internally to do a redirect passing N-abcdef
If I put these rules, it work:
RewriteRule ^/category/N-abcdef$ /category [R=301]
RewriteRule ^/category$ /category/N-abcdef [PT]
But I was trying to do something more generic, because the N-.* is different for each category. I've tried these:
RewriteCond %{REQUEST_URI} ^/(.*)/(N-.*)/?$
RewriteRule ^/(.*)/(N-.*)/?$ /$1 [R=301]
RewriteRule ^/(.*)$ /$1/%2 [PT]
These rules even remove the desired part (N-.*), But does not make the internal redirect correctly, as the reported result is not desirable.
In short, I need to hide the N-FOO URI.
Any suggestion?
An existing page is called /foo/bar.php. What I have done is a rewrite so that when a user types /foobar, it load the contents of /foo/bar.php (while keeping /foobar in the url bar)
But I also want the opposite - when a user clicks on a link or types /foo/bar.php, I want to have /foobar in the url. The reason is to avoid manually changing all the links.
How could I do that (if possible without an http redirect, but via some rewrite magic)? And is it possible for those two rules to co-exist?
Edit - After the first response, I realized my description of the problem was not proper. /foobar is not supposed to be a concatenation of foo, bar of /foo/bar.php, but an arbitrary string (/whatever).
Edit 2:
I now added RewriteRule ^whetever/?$ /foo/bar.php [L] in the / .htaccess. Then I added RewriteRule bar\.php$ /whetever [R=302,L] in the /foo .htaccess. The problem is it 's a circular reference and fails.
Thanks,
John
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/foo/[^/]+\.php$
RewriteCond %{IS_SUBREQ} !true
RewriteRule ^/foo/([^/]+)\.php$ /foo$1 [R,L]
RewriteCond %{REQUEST_URI} ^/foo[^/]
RewriteRule ^/foo(.*) /foo/$1.php [L]
The first part matches /foo/something.php and transforms them into /foosomething, but only if it is not a sub-request.
The second part takes any /foosometing and transforms it into /foo/something.php, via sub-request
You can try matching against %{THE_REQUEST} and only do the redirect when the actual request is for the php file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo/bar\.php
RewriteRule bar\.php$ /whatever [R=302,L]
RewriteRule ^whatever/?$ /foo/bar.php [L]