The Redirection of Multiple Parked Domains doesn't Work with Filename [closed] - apache

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
My problem seems simple to me but I cannot find a simple solution. Here goes: I have one main domain, and multiple domains pointing to that main domain. To avoid duplicate content I'm trying to redirect all "secondary" or "parked" domains to my main domain so that it resolves to this:
www.parkeddomain1.com => www.maindomain.com
www.parkeddomain2.com => www.maindomain.com
www.parkeddomain3.com => www.maindomain.com
And so on...
Now I have found this htaccess code that is sort of a catch-all solution (which I would prefer):
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301]
So this code works when I'm dealing only with straightforward parked domains, not with parked domains with subfolders or subfiles. So:
www.parkeddomain1.com => www.maindomain.com
redirect works fine here but when I add a subfolder this happens:
www.parkeddomain1.com/subfolder/ => www.parkeddomain1.com/subfolder/
when what I'm looking for is:
www.parkeddomain1.com/subfolder/ => www.maindomain.com/subfolder/
All this in order to avoid the duplicate content problem with search engines.
Thanks to all for any answer that would guide me to a solution.
Cheers!

If the questioner starts rewrite ruling with [R=301] 301 redirect, a.k.a. “Permanently Redirect”, and then he try to view his URL www.parkeddomain1.com/subfolder/ but the result of the rule wasn't what he want, then even he try to change the redirecting rule, his web browser will always redirect that URL into the first URL where it redirecting with [R=301] flag. Because once the browser has been redirected permanently to the wrong address, even how many times you edit the rule, your browser will still be redirected to the old address, that's a browser thing, and you may even go on to fix the rule, and then change the rule all over again without ever knowing it. Changes the 301 redirects in your browser can take a long time to show up.
The solution is to restart the web browser, or use a different one. So, if you're testing, it's better to use a [R] flag instead of [R=301] flag. And when you are 100% sure that the rule does exactly as it's expected to, then switch it to [R=301] flag. Or else, this question belongs to Server Fault.

If your sole aim is to appease search engines, you can alternatively specify a canonical URL which tells search engines the definitive URL for a piece of content. So even if the same content is served by several different query-string variants, or several different domains, the canonical URL will tell the search engine that these are just alternatives to the one true URL.
See the Google Webmaster Central Blog about Canonical URLs for the details.

Try adding the "L" after 301
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

Related

I want to set up redirects in htaccess from one domain to another but I've gone wrong somewhere

I have a website, let's say fruit.com, and currently I have a bunch of redirects set up that work just fine, so for example fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh.
I also used to have some redirects set up to allow me to use a short URL, so fru.it/mcintosh would redirect to fruit.com/apples.php?id=mcintosh.
So far so good. A few years ago, though, my short domain lapsed and I didn't renew. Recently I've purchased it again and I'm interested in getting the same setup back.
Now, though, the redirects from the short domain to the main domain aren't working, although I've used exactly the same code, so I'm at a bit of a loss for what's going wrong.
RewriteCond %{HTTP_HOST} ^www\.fru\.it$
RewriteRule ^([0-9]+)$ "http\:\/\/www\.fruit\.com\/apples.php?id=$1" [R=301,L]
although I've used exactly the same code
But the code you've posted won't redirect the stated example URL fru.it/mcintosh, since the code matches digits only, not letters.
Try the following instead:
RewriteCond %{HTTP_HOST} ^www\.fru\.it
RewriteRule ^(\w+)$ http://www.fruit.com/apples.php?id=$1 [R=301,L]
The \w shorthand character class matches upper and lowercase letters, numbers and underscore.
You don't need all the backslash-escapes in the substitution string.
Also bear in mind that the order of these directives can be important. This rule would likely need to go near the top of the .htaccess file to avoid conflicts.
Test first with a 302 (temporary) redirect to avoid potential caching issues. Clear your browser cache before testing.
Aside:
fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh
It would seem to make more sense that this would be a (internal) "rewrite", not a (external) "redirect"? The shortcode would then redirect to fruit.com/apples/mcintosh, not fruit.com/apples.php?id=mcintosh?

Will over 1000 301 redirects slow down my site? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I've updated my site links to use pretty URLs instead of query strings.
Old URL: https://digimoncard.io/deck/?deckid=1241
New URL: https://digimoncard.io/deck/green-otk-1241
Both URLs can be visited without a 404 but I plan on adding 301 redirects to my htaccess like so:
RewriteEngine on
Redirect 301 https://digimoncard.io/deck/?deckid=1241 https://digimoncard.io/deck/green-otk-1241
Part of my concern is the number of redirects needed (will be 1218 exactly). Will this potentially slow down the site/server due to having to query each of these on every page load?
My other solution is to potentially leave it alone and let google index the new URLs and overtime let the query string ones stale out.
1218 redirect directives in .htaccess shouldn't cause a noticeable/significant delay, however there are other issues with your suggestion and this can be greatly optimised to avoid any additional overhead...
...having to query each of these on every page load?
It's not just "every page load", but potentially every request. All static resources (CSS, JS, images, etc.) will trigger the same set of directives (unless they are hosted away from the application server).
RewriteEngine on
Redirect 301 https://digimoncard.io/deck/?deckid=1241 https://digimoncard.io/deck/green-otk-1241
This won't work. The mod_alias Redirect directive matches the URL-path only, it does not match the query string (or scheme+hostname), so the above directive will simply fail to match and do nothing. (RewriteEngine is also part of mod_rewrite, not mod_alias.)
In order to match the query string you need to use mod_rewrite with an additional condition to check against the QUERY_STRING server variable. For example:
RewriteCond %{QUERY_STRING} ^deckid=(1241)$
RewriteRule ^deck/$ /green-otk-%1 [R=301,L]
The %1 backreference contains 1241 (captured from the preceding CondPattern) - this simply saves repetition (that could potentially introduce bugs). Unless of course you are generating these directives automatically.
Don't use .htaccess - use your application logic instead
However, ideally, you would not be doing these redirects in .htaccess to begin with. It would be far more optimal to do these in your application logic (ideally when your site determines that the result would trigger a 404 - although that does not happen in your case). By placing these directives in .htaccess you are prioritising the "redirects" at the expense of normal site traffic. By implementing these redirects later (in your application) you prioritise normal site traffic.
Since I assume you are using a front-controller to route your URLs this should be relatively trivial to implement. (You only process the redirect logic when a request comes in that matches the old URL format.)
Optimised .htaccess version
However, you could greatly optimise this if deciding to go the .htaccess route if all your old URLs follow the same format... you could internally rewrite any request that uses the format /deck/?deckid=<number> to a subdirectory (assuming all your old URLs use this format). You then have another .htaccess file in the subdirectory that processes all the 1218 redirects. This way, you only have a single directive in your main .htaccess file, that is processed on every request and the redirect logic (in the subdirectory .htaccess file) is only processed when it needs to be.
This avoids the overhead of having 1000+ redirect directives in the main .htaccess file.
The directives in the subdirectory .htaccess file can also be simplified since we can rewrite the request to move the query string to the URL-path to avoid the additional condition later.
For example, at the top of your root .htaccess file:
RewriteEngine On
# Internally rewrite the request for (what looks like) an old URL
# ...to the "/redirect-old" subdirectory
RewriteCond %{QUERY_STRING} ^deckid=(\d+)$
RewriteRule ^deck/$ redirect-old/%1 [L]
All URLs of the form /deck/?deckid=<number> are internally rewritten to /redirect-old/<number>...
Then, in /redirect-old/.htaccess you have simplified "redirect" directives like the following that match against the rewritten URL:
# /redirect-old/.htaccess
RewriteEngine On
# Redirect old URLs
RewriteRule ^1241$ /deck/green-otk-$0 [R=301,L]
RewriteRule ^1234$ /deck/foo-$0 [R=301,L]
RewriteRule ^4321$ /deck/bar-$0 [R=301,L]
:
These directives match the rewritten URL, ie. /redirect-old/<number> and redirect accordingly.
The $0 backreference in each case is simply the URL-path (ie. number) that is matched by the RewriteRule pattern. (Saves repetition - as mentioned above.)
Well, it may slow down but not necessarily. If your .htaccess doesn't contain like 10k or more redirects, it should be fine. But on a precautionary side, you can always use files with less size and remove unnecessary redirects and let google index the URLs.
You can refer this link for more information
https://www.matthewedgar.net/do-redirects-add-to-website-speed/#htaccessredirectsspeed

Redirect and Rewrite Rule for Apache

I am trying to install a CalDAV client on my apache webserver, and I am having trouble using a combination of redirects and rewrite rules to get a desired url.
The document root for my webserver is /var/www, and the calendar files are stored in /var/www/agendav/web/public. If I go in through my browser at <website>/agendav/web/public/index.php, I have no trouble getting to the interface and using it, so that is not a problem. However, my desired URL for the calendar is <website>/calendar/, instead of having to go down through the agendav folder tree. I have been trying to perform this with a redirect rule, and a rewrite rule, but to very little success. I have found a few other answers here that have gotten me close, such as this one and this one, with a working redirect, but I am still having issues with the rewrite rule. Here is the current solution I have:
# Rules for the Calendar
Redirect "/calendar" "/agendav/web/public"
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/agendav"
RewriteRule "^/agendav/web/public/(.*?)$" "/calendar/$1"
My current solution seems a little circular. First, I redirect the user to the agendav folder, then then try to hide the redirect behind a rewrite rule, when it seems that I could just get away with a single rewrite rule. Unfortunately the group I am working for is not big enough to have their own dedicated server manager, and I ended up with the job despite knowing very little about it. Any help to get this would be greatly appreciated.
You don't need the redirect, you can do it with just a rewrite rule, the problem was that you have the condition and the rule reversed, you were using the real path for the conditions instead of the "virtual" path:
RewriteEngine on
RewriteCond %{REQUEST_URI} "^/calendar" [NC]
RewriteRule "^calendar/(.*)" "/agendav/web/public/$1"
With this rules all requests for http://yourwebsite/calendar/* are internally served with http://yourwebsite/agendav/web/public/*.

Apache - redirect only the domain URL to a subpage

I have a shared hosting Apache server, and I'm trying to send visitors who come to the main domain URL to a specific page, with the URL replaced and a 303 redirect:
example.com
example.com/
to
example.com/subdirectory/page.html
Only the plain domain URL should get redirected, not:
example.com/page.html
example.com/otherdirectory
example.com/otherdirectory/
example.com/subdirectory
example.com/subdirectory/
example.com/subdirectory/otherpage.html
example.com/subdirectory/otherdirectory
example.com/subdirectory/otherdirectory/
example.com/subdirectory/otherdirectory/page.html
I'm not sure that RewriteEngine is allowed on this server, if there are alternative approaches possible.
The crude way I've thought of is to just use DirectoryIndex to send the visitor to example.com/index.php -- and have a PHP redirect in that file go where I want. But I'm not sure if this might produce a visible blip for some visitors, or how Google would feel about it.
I've found other instances of this kind of question on Stack Overflow, but the answers are failing for me in some way or another. As the behavior is not intuitive, testing before posting might be advisable.
Thanks
If mod_rewrite is not enabled you can use mod_alias based rule like this in your DocumentRoot/.htaccess:
RedirectMatch 303 ^/?$ /subdirectory/page.html
UPDATE: Equivalent mod_rewrite rule:
RewriteEngine On
RewriteRule ^/?$ /subdirectory/page.html [L,R=303]

example.com./ vs example.com/ create problems with visitor tracking programs

I tried to search this but due to the search terms I can not find an answer anywhere.
My visitor tracking site (I use Piwik) can not decipher between the following issue below.
example.com./ vs example.com/
example.com./ when types in to find a website is of course wrong, somehow others get to my site and every page they visit shows as an external link and this ./ problem.
How can I in .htaccess (in my root of my domain) add a rule to fix this problem and possibly others that are similar to this?
This is not something that is fixable from htaccess, as the example.com. hostname is invalid and any request for that will never reach your server. There's no way to rewrite outgoing requests from the browser to piwik because those requests have nothing to do with your server and thus anything htaccess wouldn't be applicable.
You need to figure out why browsers are sending tracking information to piwik with the hostname example.com.. Just as a wild uneducated guess, it may have to do with some inelegant appending of hostname and badly formed relative URI's like <a href="./my/page.html">. So when the code unintelligently appends them together, you get example.com./my/page.html.
Try using the HTTP_HOST variable, something like the following might work.
RewriteEngine on
Rewritecond %{HTTP_HOST} ^www\.example\.com\.
RewriteRule (.*) http://www.example.com/$1 [R=301,L]