Because of the way that my file structure was set up, I have folders that I don't want showing in the URL.
I want to redirect this URL:
https://portal.domain.com/portal.domain.com/portal/test.php
To this:
https://portal.domain.com/test
I have tried this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^portal.domain.com
RewriteRule ^(.*)$ https://portal.domain.com/portal.domain.com/portal/$1 [L,NC]
But I get a 'too many redirects' message.
the rewrite rule: RewriteCond %{HTTP_HOST} ^portal.domain.com
is too generic, then:
1) the user open https://portal.domain.com/portal.domain.com/portal/test.php
2) the rewrite rule match and redirect to https://portal.domain.com/test.php
3) but when the user request the page https://portal.domain.com/test.php
the rewrite rule match again and will redirect the user to https://portal.domain.com/test.php
4,5,6,7,8,9.....) then the user opens the same page and the rewrite rlue match again and again in an infinite loop.
Solution: the condition RewriteCond %{HTTP_HOST} ^portal.domain.com must be less generic and not math the redirect location.
Related
A client has asked us to replicate all the content from an old domain (bcsbd.com) to their main domain (ywcacam.org), and also create redirects so the old URLs are still functional. Unfortunately, the URLs aren't exact matches, e.g., [olddomain]/about has become [newdomain]/about_soo_bahk_do. There are less than 10 specific URLs to handle, which we initially did successfully using Redirect statements in the old domain's htaccess file:
# redirect specific pages to page on new domain
Redirect /about http://www.ywcacam.org/about_soo_bahk_do
We also need a catch-all, so that any other requests go to a specific URL on the new domain, e.g., www.bcsbd.com/somefile becomes www.ywcacam.org/soo_bahk_do. We handled this using Rewrite statements:
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
Quick research showed the Rewrite directives (using mod_rewrite) would always be processed before the Redirect directives (using mod_alias). So we replaced the Redirects with Rewrites:
Options +FollowSymlinks
RewriteEngine On
RewriteRule /about http://www.ywcacam.org/about_soo_bahk_do [L]
RewriteRule /programs http://www.ywcacam.org/programs_soo_bahk_do [L]
...
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
The problem is that just the catch-all is working - the new Rewrite rules are being ignored. What are we doing wrong in those statements?
Thanks in advance for the help!
I want a redirect to happen if subfolder prefix is 'blog' and 'X' is not equal to 'party'.
This should not redirect
http://www.hostname.co.uk/blogs/blogparty
This should redirect
http://www.hostname.co.uk/blogs/blogX
to
http://www.hostname.co.uk/blogs/X
where 'X' is any string which does not equal to 'party'.
This should work on sub directories of this directory as well meaning this should be redirected too.
http://www.hostname.co.uk/blogs/blogX/blah/index.php
to
http://www.hostname.co.uk/blogs/X/blah/index.php
This is what i tried which is redirecting everything including 'blogparty'
# Redirect blogBLOGNAME to BLOGNAME excluding blogparty
RewriteCond %{REQUEST_URI} !^/blogs/blogparty/.*
RewriteCond %{REQUEST_URI} !^/blogs/blogparty$
RewriteRule ^/blogs/blog(.*) /blogs/$1 [R=301,L]
Your rules should have worked. I believe there are other routing rules below your shown rule. To handle this you can have your rule based on THE_REQUEST instead (which doesn't change after other rules):
RewriteCond %{THE_REQUEST} !\s/+blogs/blogparty(/\S*)?\s [NC]
RewriteRule ^(blogs)/blog(.+)$ /$1/$2 [R=302,L,NC,NE]
I have subfolder on my server with a case sensitive name: http://www.domain.com/MySubFolder/page.html
I just recently found out that trying to access the same page via http://www.domain.com/mysubfolder/page.html returns a 404 error.
Is there anything I can do to change mysubfolder to MySubFolder? I tried the following:
RewriteCond %{REQUEST_URI} /mysubfolder [NC]
RewriteRule .* http://www.domain.com/MySubFolder/ [R=301,L]
This corrects the casing issue when a user enters the URL lowercase, but it does not redirect to the proper page. In other words, entering either domain.com/MySubFolder/page.html OR domain.com/mysubfolder/page.html brings the user to domain.com/MySubFolder. I want to keep the final part of the url (page.html) while only correcting the subfoldername.
You can use this rule for getting the right URL in place:
RewriteCond %{REQUEST_URI} ^/MySubFolder/page\.html$ [NC]
RewriteRule !^MySubFolder/page\.html$ /MySubFolder/page.html [R=301,L]
Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.
I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113¶m=value will not be redirected because query string is group=113¶m=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]