Convert NGINX rewrite rules to Apache htaccess [closed] - apache

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I have a client who is moving away from an NGINX webserver to Apache. Everything is simple nothing complicated however since I'm an NGINX kinda guy I forgot how to convert the NGINX rewrite rules onto apache ones.
For example, these are the NGINX rewrites
rewrite ^/Tower-Topics-Calendar/?$ https://$host/events/ permanent;
How would I convert something like that onto .htaccess to use with Apache?

rewrite ^/Tower-Topics-Calendar/?$ https://$host/events/ permanent;
This looks like an external 301 redirect from /Tower-Topics-Calendar (with and without a trailing slash) to https://<host>/events/ - where <host> is the same hostname from the request and you specifically state the HTTPS protocol in the target.
In .htaccess you can achieve this using mod_rewrite. For example:
RewriteEngine On
RewriteRule ^Tower-Topics-Calendar/?$ https://%{HTTP_HOST}/events/ [R=301,L]
Note the absence of the slash prefix in the RewriteRule pattern.
However, if you don't specifically need to include the HTTPS scheme (ie. this is already canonicalised) then you can use a single mod_alias RedirectMatch directive instead. For example:
RedirectMatch 301 ^Tower-Topics-Calendar/?$ /events/
OR, to include the HTTPS protocol, you need to hardcode the hostname:
RedirectMatch 301 ^Tower-Topics-Calendar/?$ https://example.com/events/

Related

Error 500 with .htaccess edits but mod_rewrite is loaded [closed]

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
as of title, I'm having problems with my .htaccess file, everything should be set fine, but has I write something as basic as
RewriteEngine on
it starts giving me that nice 500 Internal Server Error. I'm hosting on localhost on an Apache server (UNIX)
Obviously I triple checked that everything is set fine, and top of all that mod_rewrite is loaded.
Thanks for your precious help!
If the error occurs for every single type of instruction you put in the file (that is, caching, FilesMatch, ErrorDocument, etc), there are two possible options that I can think of right now:
The encoding of your .htaccess file is not compatible with the server you're running. Try converting it to ANSI, and then try again (Apache does not support Byte Order Marks, so you'd need to save it to ANSI, or UTF-8, without the BOM). If that does not work:
AllowOverride is not set correctly, or not at all. If you have access to the Virtual Host/Directory configuration, you'll need to enable it by adding the line AllowOverride All in the <Directory> container.
When you try to remove the code RewriteEngine on and the URL remapping or the URL redirecting is still functioning well, then you do not have to put that code in your .htaccess file, for your HTTP server was already configured that the rewrite engine is on. But if you've tried to remove it and the rewriting or the redirecting stop, then you must include all your .htaccess codes in your question. Then if it's empty, then why you need to turn the RewriteEngine on if you're not going to apply even a single RewriteRule?
Have you ever tried this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^notexist.html$ /index.html
And try to visit yourdomain.com/notexist.html if it's URL remapping into /index.html.

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

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]

url rewrite `/abc/products-` to `/def/products-` [closed]

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 10 years ago.
Improve this question
I have many url links in /abc/ sub-directory, like
http://www.domain.com/abc/products-12345
http://www.domain.com/abc/products-23456
http://www.domain.com/abc/products-34567
http://www.domain.com/abc/new-items
Now I want to url rewrite /abc/products- to /def/products-
http://www.domain.com/def/products-12345
http://www.domain.com/def/products-23456
http://www.domain.com/def/products-34567
http://www.domain.com/abc/new-items
My code in .htaccess, but nothing changed. How to rewrite in this case? Thanks.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^abc/products-(.*)$ /def/products-$1 [L,R=301]
</IfModule>
As you can test on this simulator the rules should work.
The most probable problem you are facing should be:
The global configuration does not allow for .htaccess overwrite
.htaccess is not readable for apache user
You have no mod_rewrite active on this apache server, thus no trigger to <IfModule> directive.
Best troubleshooting option would be trying a redirection of all page to a static page. If this does not work, look for a configuration problem.

Nginx vs Apache or using Apache with nginx [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 9 years ago.
Improve this question
I have been running a website which servers javascript widgets for about 2 years. Now my question is whether I should use nginx purely or I should continue using apache with nginx.
I have about 200 sign ups a day and that means that sometimes the request rate for widget goes up by 2000 a day. So, now the problem is switching to nginx means that i would not be able to use the rewrite rules that i am using in apache.
Now that is one problem I know of, but are there any other issues that I can expect to see in an nginx environment that I dont in Apache?
Would you suggest me to switch purely to nginx or stay with apache and nginx as reverse proxy?
You could still use the rewrite rules from Apache, with slight modifications (I took this from Nginx Primer):
Apache:
RewriteCond %{HTTP_HOST} ^example.org$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Nginx:
if ($host != 'example.org' ) {
rewrite ^/(.*)$ http://www.example.org/$1 permanent;
}
Another issue is .htaccess files, but this would only be an issue if your sharing the server with others.
I would also research any Apache modules that your relying on and ensure that the Nginx equivalents include the same functionality. Definitely test your webapp with Nginx in a test environment first to identify any issues.
In the end, if your goal is better performance then the migration from Apache to Nginx should be worth it.

Redirect ALL requests under a domain to static page [closed]

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 13 years ago.
Improve this question
I am trying to redirect ALL requests for mydomain.com whether they are something like:
http://www.mydomain.com
http://mydomain.com
http://mydomain.com/photos
http://mydomain.com/index.php?id=672
to be redirected to
http://mydomain.com/index.html
As long as it has mydomain.com in it, they should see this page - its a we'll be back soon message.
Should I do it in .htaccess or conf? How?
I actually ended up finding the answer on ServerFault:
https://serverfault.com/questions/32513/url-redirect-to-another-page-on-the-same-site
"This example will 302 redirect all URLs to "/underconstruction.html":
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/underconstruction.html
RewriteRule ^ /underconstruction.html [R=302]
(which translates as "If URI is not /underconstruction.html, redirect to /underconstruction.html")" - Tommeh