301 .htacess redirect issue - apache

I'm making a website, (for examples sake it will be http://www.example.com).I am trying to make it redirect http://example.com/jquery/releasenotes to http://blog.jquery.com/?s=release+notesBy using this it put a / on the end, this stops it from working, since it searches for the / aswell. I tried using http://goo.gl and converted it to http://goo.gl/WdiItL Somehow this also fails to remove the / when in redirect. However if you simply go to http://goo.gl/WdiItL it works. My .htaccess file looks like this:
Redirect 301 /jquery/releasenotes http://goo.gl/WdiItL
I have no idea how to make it not have the / on the end.Any help would be hugely appreciated!

Use this RedirectMatch rule:
RedirectMatch 301 ^/jquery/releasenotes/?$ http://goo.gl/WdiItL
Make sure to clear your browser cache before testing this

I think you have to use mod_rewrite's %{QUERY_STRING}
for example:
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteCond %{QUERY_STRING} ^(par1=1&par2=2)
RewriteRule ^$ http://alt.example.com/?%1 [R=301,L]

Related

.htaccess redirect to subdomain url with hash

I'm trying to redirect a site with the following url structure:
https://example.com/2021/about
to
https://2021.example.com/#about
https://example.com/2021/visit
to
https://2021.example.com/#visit
how can i do this?
i tried adding this to the /about directory in the original domain:
RewriteEngine on
RewriteBase /
RewriteRule (.*) https://2021.example.com/#about [R=301,NE, L]
but what i got after the redirect was
https://2021.example.com/#about2021/about
which is not right. any help is appreciated
[EDIT] i only want to apply this to some folders, like /2021/about and 2021/visit
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^(20\d{2})/(.+?)/?$ https://$1.%1/#$2 [R=301,L,NE]
Make sure this is your topmost rule in .htaccess and you clear your browser cache before testing this new rule.
Could you please try following, written based on your shown samples. Please make sure to clear your browser cache before testing your URLs. Following will only be applied to 2021/about OR 2021/visit uris.
RewriteEngine ON
RewriteCond %{HTTP_HOST} (example\.com) [NC]
RewriteRule ^(2021)/(about|visit)/?$ http://$1.%1/#$2 [R=301,NC,NE,L]

Htaccess Redirect URL with two forward slashes (not double) won't work

I want to redirect from one domain to a new domain. At the same time, the URL structure has changed.
Old: https://www.olddomain.com/parentpage/oldtitle/
New: https://www.newdomain.com/newtitle
This is wordpress, and I placed this code above the Wordpress stuff, as well as tested it here: https://htaccess.madewithlove.be/
I tried this, which doesn't work:
Redirect 301 /parentpage/title https://www.newdomain.com/newtitle
Also, when testing it at https://htaccess.madewithlove.be/, I do have this redirect:
Redirect 301 /parentpage https://www.newdomain.com/parentpage
The tester would skip my preferred redirect above, and use this one, leaving me with this, which does not exist:
https://www.newdomain.com/parentpage/oldtitle
Even when I place the preferred redirect above this one. I need both, unfortunately.
Have also tried the following RewriteRules (not all at the same time)
ReWriteRule https://www.olddomain.com/parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule /parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule "https://www.olddomain.com/parentpage/oldtitle/" "https://www.newdomain.com/newtitle"
I think it has something to do with that second forward slash separating the parentpage name and page title, but I can't figure out how to fix it.
In RewriteRule it wouldn't match http or https in it, you may try following.
please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/parentage/oldtitle/?$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/newtitle [R=301,L]

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

Basic URL rewrite with mod_rewrite

I would like to rewrite the URL of one of my pages from
http://www.mydomain.com/some/application/page.html
to
http://www.mydomain.com/apply
I believe this code will work. But in 301 redirects, you often see [R=301,L] or some version of that appended to the end of the rewrite rule - is the code below the best way to perform the redirection and will Google understand it?
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /some/application/page.html?=$1 [L]
I think you want to do this:
The URL that your users see (even google):
http://www.mydomain.com/apply
to
http://www.mydomain.com/some/application/page.html
the internal(actual) URL.
then I would suggest you to go this:
RewriteEngine On
#condition to redirect
RewriteCond %{REQUEST_URI} ^/?apply/?$
RewriteRule ^/?apply/?$ /some/application/page.html [L]
The flag [L] ( Flag L docs ) signifies RewriteEngine to stop rewriting any rules further. This will not perform a permanent redirection.
You don't need a [R=301] flag here. 301 is for permanent redirection. Use the short URL everywhere.
To my knowledge, you will need [R=301,L]to do a redirect properly. Apache2 by default will use a 302 redirect so if it is a permanent redirect you should force R=301, as you noted. The documentation for RewriteRule is unclear if [L] alone will always perform a 301 redirect. Be safe, tell apache exactly what to do :-).

301 redirect .htaccess

Im trying to request the following entire site 301 redirect:
word.something.blah.domain.com --> http://www.word.com
I don't know how to write the 301 redirect rule.
Can someone help out?
I will assume you are using the same directory to serve files on both domains. In which case, a Redirect clause won't work (infinite redirect loop).
With mod_rewrite, you can check the value of the current HTTP_HOST and take a decision based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.something\.blah\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,NE,L]
put this into root directory of the subdomain:
Redirect permanent / http://www.word.com
If you are keeping everything else the same - that is, the file names - but simply changing the domain, this code is all you need to put on the OLD DOMAIN htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.co.uk
RewriteRule (.*) http://www.newdomain.co.uk/$1 [R=301,L]