How can I make htaccess Dynamic and Static rules? - apache

I have this dynamic rule:
RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]
and I am trying to add some static rules like this:
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools
The problem is when I type the following in the address bar:
http://www.{samedomain}.com/construction/pools.html
then apache redirects to:
http://www.{samedomain}.com/construction-services/pools?sub=construction&second=pools.html
What I want is apache redirects to:
http://www.{samedomain}.com/construction-services/pools
Does anybody know why?
Thank you.

Redirects are processed in the order they appear, so it should work to place the static redirect before the RewriteRule. Don't for get the [L] flag on your RewriteRule.
RewriteEngine On
# Match the static redirect first
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools
# Since your dynamic URLs don't end in .html, avoid those with RewriteCond
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^([^/]+)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC,L]
Or you could do it without the RewriteCond if none of your dynamic urls have a .
RewriteRule ^([^/]+)/([^.]+)$ /rewrite.php?sub=$1&second=$2 [NC,L]

Write the htaccess rule based on priority ( place the rule which has common behavior at last)
In your case
RedirectMatch 302 /construction/pools.html http://www.{samedomain}.com/construction-services/pools
RewriteRule ^(.*)/(.*)$ /rewrite.php?sub=$1&second=$2 [NC]

Related

htaccess rule for redirecting when parameters are present

RewriteEngine On
RewriteRule ^$ /index.phtml [R,L]
RewriteRule ^index.phtml.+$ /index.php$1 [R,L]
I have 2 urls like https://www.example.com/index.phtml and https://www.example.com/index.php which both point to very different sites. I want to redirect to index.php in case there are any parameters found after index.phtml.
For example, https://www.example.com/index.phtml?a=b should go to https://www.example.com/index.php?a=b (second rule). I am trying the above rules but the second one isn't matching (the first rule works fine). Thanks for any help/suggestions
Please ask if you need any more details.
You may use this code in site root .htaccess:
RewriteEngine On
# if there is a query string then redirect
# /index.phtml to /index.php
RewriteCond %{QUERY_STRING} .
RewriteRule ^(?:index\.phtml)?$ /index.php [L,NC]
# redirect landing page to /index.phtml
RewriteRule ^$ /index.phtml [R=302,L]
Note that QUERY_STRING get automatically appended to target URL so /index.phtml?a=b will be redirected to /index.php?a=b.
Make sure to clear your browser cache before testing this change.
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your redirect rules.
With your shown samples, please try following .htaccess rules file. Please make sure that your htaccess file and index.php are present in same folder.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##For without query string rules.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.phtml/?$ index.php [R=301,NC,L]
##For query string rules.
RewriteCond %{THE_REQUEST} \s.index\.phtml\?([^=]*=\S+)\s [NC]
RewriteRule ^ index\.php?%1 [R=301,L]

.htaccess redirect only if GET parameter exist

I have the following line in my .htaccess file:
Redirect 301 /folder1 https://www.example.com/folder2/file.php
This will redirect everything from /folder1 to https://www.example.com/folder2/file.php.
I need a condition to only allow this redirection if the URL contains a mykey= GET parameter, else ignore this redirection command.
How can I do that?
You cannot do this using Redirect directive that does basic URI matching.
You will need to use mod_rewrite based rules for this like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)mykey= [NC]
RewriteRule ^folder1(/|$) /folder2/file.php [R=301,L,NC]
Make sure to clear your cache before testing.
References:
Apache mod_rewrite Introduction
I finally found the a working solution:
RewriteCond %{REQUEST_URI} ^/folder1/
RewriteCond %{QUERY_STRING} mykey=
RewriteRule ^folder1\/$ /folder2/file.php$1 [R=301,L]

.htaccess 301 redirect sub domain query

I need to 301 redirect this:
/blog/?=31
to
/blog/
and
/blog/?page_id=2
to
/blog/
I've tried the obvious stuff:
Redirect 301 /blog/?p=31 /blog/
Redirect 301 /blog/?page_id=2 /blog/
And obviously it didn't work otherwise I wouldn't ask this question here, so don't state the obvious!
Redirect directive is provided by mod_alias. I'm not sure, but I think that it does not receive the query string parameter. Using mod_rewrite though, the rules would be:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=31|page_id=2$
RewriteRule ^/?blog/?$ /blog/? [R=301,L]
In order to handle redirects involving Query Strings, you will need to use a mod_rewrite RewriteCond %{QUERY_STRING} in your .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^blog http://example.com/blog? [NC,L,R=301]

.htaccess Rewrite Rules, friendly URLs + redirect

I'm using .htaccess to take URLs that look like:
/index.php?page=2
and rewrite them to, for example:
/contact-us
I want to do two things:
When loading /contact-us, show page /index.php?page=2 but keep the friendly URL. I know this looks something like:
RewriteRule ^contact-us$ "index\.php\?page\=2" [L]
Which does work ok. But now I also want people who navigate to /index.php?page=2 to end up on /contact-us - how do I achieve this as a 301 redirect in combination with the friendly URL rewrite?
Not sure why you are using quotes and escapes in target portion of your rewrite rule. The target portion is not a regular expression. That can simply look like:
RewriteRule ^contact-us$ /index.php?page=2 [L]
To redirect the index.php?page=2 you will need to do the following. This rule MUST be before the rule above or you will get in rewrite loop.
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=2$
RewriteRule .* /contact-us [R=301,L]
Here you are setting two rewrite conditions: that the page is /index.php and that the query string is page=2 (and only page=2). The R=301 flag will force an external rewrite to /contact-us and also send and HTTP 301 header.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?page=2[&\s] [NC]
RewriteRule ^ contact-us? [R=302,L,NE]
RewriteRule ^contact-us/?$ index.php?page=2 [L,QSA,NC]

htaccess rewrite base url

Anyone out there good with htaccess and mod rewrite - i need your help!
I need to rewrite the base part of a url.
for example all requestst to http://domain1.com need to go to http://domain2.com
The requests will typically be in the form as follows:
http://domain1.com/main/test?q=1
i then need to go to http://domain2.com/main/test?q=2
Pleas help!
Thanks in advance
Try this in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
# redirect for http
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^/?(.*)$ http://domain2.com/$1 [R=301,QSA,L,NE]
# redirect for https
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/?(.*)$ https://domain2.com/$1 [R=301,QSA,L,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters
$1 is your REQUEST_URI
Rewriting URL's across multiple domains? I'm not entirely sure this can work, considering Apache's request-handling algorithm. You're looking for a redirect, not rewrite.