301 Redirect with .Htaccess - .htaccess

my htaccess file has these lines:
RewriteRule ^newlink ?do=something
Redirect 301 /oldlink/ /newlink
when i write http://www.example.com/oldlink/, browser redirects to http://www.example.com/newlink?do=something
how can i trim ?do=something ??
it should redirect to /newlink; not /newlink?do=something

Don't mix mod_rewrite rules with mod_alias ones. Keep your rules using mod_rewrite itself like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+newlink[\s?] [NC]
RewriteRule ^ /?do=something [L,NC,QSA]
RewriteCond %{THE_REQUEST} \s/+oldlink/?[\s?] [NC]
RewriteRule ^ /newlink? [L,NC,R=302]
? in the end is for trimming any existing query string.

Related

php - Htaccess as fake directories

I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]

How to 301 redirect htaccess with & sign

From /?act=view&id=NUMBER to URL
examples:
/?act=view&id=1 to http://google.com
/?act=view&id=2 to http://facebook.com/blabla
My code
Redirect 301 /?act=view&id=157 http://google.com
You can not manipulate query strings using a Redirect directive, you need to use mod-rewrite,
Try :
RewriteEngine on
#1)Redirect "?act=view&id=1" to "http://google.com/"#
RewriteCond %{THE_REQUEST} \?act=view&id=1 [NC]
RewriteRule ^ http://google.com/? [NC,L,R]
#2)Redirect "?act=view&id=2" to "http://facebook.com/"#
RewriteCond %{THE_REQUEST} \?act=view&id=2 [NC]
RewriteRule ^ http://facebook.com/? [NC,L,R]

Advanced htaccess rewrite

i want to redirect all links with this format
https://besthost.tn/aze/viewticket.php?tid=VARIABLE1&c=VARIABLE2
to
https://besthost.tn/client-2/?ccce=viewticket&tid=VARIABLE1&c=VARIABLE2
the VARIABLE1 and VARIABLE2 must be inchanged while the redirection.
thank you
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /viewticket.php\?tid=([^&]+)&c=([^\s&]+) [NC]
RewriteRule ^ /client-2/?ccce=viewticket&tid=%1&c=%2 [NC,L,R]
This will work from the root and identify '/aze/viewticket.php' and then the tid=X&C=X pattern for redirection:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/aze/viewticket\.php [NC]
RewriteCond %{QUERY_STRING} ^tid=(.+)&c=(.+)
RewriteRule ^(.*)$ /client-2/?ccce=viewticket&tid=%1&c=%2 [L,R=301]
The R=301 part indicates a 301 'permanent' redirect which will be cached by the browser and is required if this is a permanent change in your website (for performance reasons).

remove a query string in htaccess 301 redirect

I have URL like these:
/one/two?page=1
/three/four/five?page=2
I need to remove "page=1" and redirect it to /one/two but only when "page=1" appears, not "page=2".
How can i do that?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=1$ [NC]
RewriteRule %{REQUEST_URI}? [L,R]

htaccess weird revert happening with a url rewrite rule and a 301 redirect

I'm having issues trying to do a 301 redirect in my htaccess file. It is having a problem working with a part of my rewrite rules I have created. In summary, the rewrite strips the extension off of the page and replaces the query ?primary-view=city to /city. Here is my htaccess file.
RewriteEngine On
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
# Redirect /chicago-downtown to /chicago
Redirect 301 /chicago-downtown to /chicago
So the url after the rewrite will look something like
#Original url
example.com/page.php?primary-view=chicago-downtown
#Url rewritten properly
example.com/page/chicago
If I type in example.com/page/chicago then it works fine. If I type in example.com/page/chicago-downtown its currently redirecting to the following:
#Current bad redirect from example.com/page/chicago-downtown
example.com/page/chicago?primary-view=chicago-downtown
#Desired redirect from example.com/page/chicago-downtown
example.com/page/chicago
I have tried man things and came to the conclusion that it is the following line causing the issue with the 301 redirect.
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
Is there some way I can rewrite my rewrite rules, or is there a rewrite condition I need to use to prevent this from happening?
You don't need Redirect rule. Just an additional mod_rewrite rule for chicago-downtown => chicago:
RewriteEngine On
RewriteRule ^(page/chicago)-downtown/?$ /$1 [L,R=302,NC]
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
This is probably caused because you're mixing mod_alias directives with mod_rewrite directives. That means both directives get applied to the same request in an unwanted way. Remove the Redirect directive (mod_alias) and user a rewrite rule instead. But you need to add the rule before your other rules (i.e. right after RewriteEngine On)
RewriteRule ^chicago-downtown(.*)$ /chicago$1 [L,R=301]

Resources