301 .htaccess redirection apache server - regex

So I am having issue I want all requests from
https://transfinmedia.com/author?url=akchopra1-A268
to be 301 redirected on
https://transfinmedia.com/author/akchopra1-A268
but when i use
RewriteEngine on
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^author(.*) /author/%1 [L,R=301,NC]
requests to
https://transfinmedia.com/author?url=akchopra1-A268 gets redirected on
https://transfinmedia.com/author/akchopra1-A268?url=akchopra1-A268
what am i doing wrong here, completely out of clue.

By default ,mod-rewrite appends old QueryString to the new target url. To discard QueryString ,you need to put a ? (an empty question mark) at the end of the target url.
RewriteEngine on
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^author(.*) /author/%1? [L,R=301,NC]
Make sure to clear your browser cache before using this.

change RewriteRule to
RewriteRule ^author\?url=(.*)$ /author/%1 [L,R=301,NC]
Demo

Related

Apache Redirect URI Requests

I'm trying to redirect http requests that contain a specific URI to a different domain with a different URI completely. Redirecting the top level domain works but I can't seem to get the URI rules to redirect.
In essence it should act as follows:
If the url request is:
www.example.com/unique-URI
it needs to redirect to:
https://example2.com/anotheruniqueURI
Currently I have this:
RewriteEngine On
#This redirect works successfully
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example2.com/something [R=301,L]
#This attempt to redirect requests with the specific URI does not work.
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteCond %{REQUEST_URI} ^/cars-application$ [NC]
RewriteRule ^/(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
I've tried many different combinations inside my RewriteRule such as explicitly stating the URI like I did in the RewriteCond above but that doesn't work. Using $1 here won't apply since I'm redirecting to a completely different domain and URI. The URI's I am expecting will be unique. Could you guys provide me some pointers. Is my regex correct or is my rewrite rule capture just wrong?
Your rule failed to work due to the leading slash in your RewriteRule's pattern . Remove the slash to fix it.
RewriteRule ^(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
Assuming you are redirecting from within a virtualhost of the first domain, you may just do the following:
Redirect permanent /unique-URI http://www.domain2.com/newlocation

htaccess rewrite from url query

I need some help trying to redirect a query to a url.
www.example.com/?categoryID=79
needs to redirect to
www.example.com/catname
where catname is just a string, it has no variables.
Here's what I tried so far:
I began with a humble approach that failed horribly:
redirect 301 /?CategoryID=79 http://www.example.com/catname/
then i moved on to mod_rewrite :
RewriteCond %{QUERY_STRING} CategoryID=79$
RewriteRule (.*) /catname/? [R=301,L]
Both did not work and I'm actually stomped.
any help would be appreciated.
Just to be clear, In the end i'll have many of these rules redirecting to various category names.
important - it's not enough for /catname to display the proper page, the request with the query parameter must redirect to the new url.
This rule should work for you as first rule in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?CategoryID=79[&\s] [NC]
RewriteRule ^ /catname/? [R=302,L]

htaccess subdomain redirect to port while keeping original url

I have trouble getting this to work properly, what I'm trying to do is make
http://subdomain.domain.com redirect to domain.com:8080 while keeping the original url
"subdomain.domain.com"
Code so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain.domain.com
RewriteRule ^(.*)$ http://%1domain.com:8080$1 [L]
Which does the redirect, but browser url changes to "http://domain.com:8080" which is not what I seek.
Thank you in advance!
For this to happen you need to enable mod_proxy in subdomain\.domain\.com. Once it is enabled try this rule in DocumentRoot/.htaccess of subdomain.domain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.(domain\.com)$
RewriteRule ^ http://%1:8080%{REQUEST_URI} [L,NE,P]

unable to come up with htaccess rewrite engine

I want to redirect domain1.com to domain2.com in all cases except one particular case:
domain1.com/subfolder/index.php
I want this domain1.com/subfolder/index.php to be intact and not get redirected to domain2.com because I have hundreds of users already bookmarked this page.
But anything and everything besides that domain1.com/subfolder/index.php, I want domain1.com to be redirected to domain2.com
Please help.
Have the following .htaccess in your web root /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)domain1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
Use just [L] if you want the redirection to be transparent i.e. without letting your visitors know.
Use !^/subfolder/.*$ if you want to stop redirection for the complete folder as well as its contents.
You can use RewriteCond to check to see the requested uri is anything but the one you want to redirect, capture the desired path and then redirect to the second domain.
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$
RewriteRule (.*) http://domain2.com/$1

Tricky URL rewrite, regex .htaccess

I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?