.htaccess Rewrite Rules, friendly URLs + redirect - apache

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]

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 - mod-rewrite and redirecting profile URLs with parameters to clean ones

I have links to user profiles like:
https://example.com/chat/index.php?action=user&member=547
https://example.com/chat/index.php?action=user&member=11540
etc.
My goal is to mod-rewrite such URLs so that they look nicer, ie. they should look like:
https://example.com/chat/member-547/
https://example.com/chat/member-11540/
etc.
And URLs without a trailing slash, ie.
https://example.com/chat/member-547
should be 301-forwarded to one with slash, ie.
https://example.com/chat/member-547/
So in .htaccess I tried this, but only the first line seems to work and it's not complete:
RewriteRule ^chat/member-([0-9]+)/$ ./index.php?action=user&member=$1
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
TO SUMMARIZE:
When someone enters URL like:
https://example.com/chat/index.php?action=user&member=547
it should be 301-redirected to:
https://example.com/chat/member-547/
When someone enters:
https://example.com/chat/member-547
it should also be 301-redirected to:
https://example.com/chat/member-547/
I hope there's an efficient way to do it right.
Starkeen's answer is correct, and will work. I'll just suggest an alternative, which acts on the raw request variable itself:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /chat/index\.php\?action=user&(member)=(\d+) [NC]
RewriteRule ^chat/index\.php$ /chat/%1-%2/? [R=301,L]
RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
RewriteRule ^chat/(member)-(\d+)/$ /chat/index.php?action=user&$1=$2 [L]
You can use the following rule :
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^action=([^&]+)&member=(.+)$
RewriteRule /chat/index\.php$ /chat/member-%2/? [L,R=301]
RewriteRule ^chat/member-(.+)/?$ /chat/index.php?action=user&member=$1 [L]

htaccess rewrite rule not working for http://

My rewrite rule is
RewriteRule ^site/([^/]+)$ page.php?site=$1 [L]
It is working fine for
http://example.com/site/google.com
But not working when i add http:// or https:// before google.com like
http://example.com/site/http://google.com
OR
http://example.com/site/https://google.com
Please share a rewrite rule which can accept everything after /site/ like http://example.com/site/here_it_may_be_any.
You need to capture http:// type string from RewriteCond otherwise Apache strips multiple slash to single in RewriteRule pattern. Use this rule:
RewriteCond %{REQUEST_URI} /site/(.+)$ [NC]
RewriteRule ^ page.php?site=%1 [L,QSA]

Rewrite rule for prepending /something to a root URL with query strings

I have a URL as follows:
www.xyz.com/?_ARGS=/string/_includes/header/header.jsp_AF&_dynSessConf=1234567890&/atg/userprofiling/ProfileFormHandler.logout=true&_D%3A/atg/userprofiling/ProfileFormHandler.logout=+
I need to add /something after the root url to convert to the following form:
www.xyz.com/something/?_ARGS=/string/_includes/header/header.jsp_AF&_dynSessConf=1234567890&/atg/userprofiling/ProfileFormHandler.logout=true&_D%3A/atg/userprofiling/ProfileFormHandler.logout=+
I have the rewrite condition figured out where logout=true is the main keyword. But I am not sure what the rewrite rule should be.
RewriteEngine On
RewriteCond %{QUERY_STRING} logout=true
RewriteRule [QSA,NC,NE,R=301,L]
I need your suggestion guys.
Thanks.
If your first step in rewriting is to simply insert /something at the beginning of any URI that lacks it,
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/something [NC]
RewriteRule ^(.*)$ /something/$1 [L]
could be enough. From there, you can do other rewrites as needed.

How can I make htaccess Dynamic and Static rules?

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]