How to rewrite this Apache rule on Nginx?
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} ^.*(pdf|epub)$
RewriteRule ^(.*)$ /wp-content/themes/divi-child/download.php?file=$1 [L]
</IfModule>
The Apache mod_rewrite rule you specified redirects requests for files ending with the suffix pdf or epub to /wp-content/themes/divi-child/download.php?file=, with the filename appended to the end of the URI.
Using nginx, this could be accomplished with the following location block within your virtual host configuration:
location ~* \.(pdf|epub)$ {
rewrite ^/(.*(?:pdf|epub))$ /wp-content/themes/divi-child/download.php?file=$1;
}
This Stack Overflow question offers a bit of insight into how the pattern matching works for file extensions.
For more information, check out the nginx documentation and blog posts.
Related
I have a lot of page URLs like domain.com/page I want a rewrite rule that will change all my pages URLs as page.domain.com meaning whatever will come after the domain just rewrite it as a subdomain
example urls
expertpro.cloud/hot-to-write-blog to hot-to-write-blog.expertpro.cloud
expertpro.cloud/game to game.expertpro.cloud
expertpro.cloud/nibm-full-form to nibm-full-form.expertpro.cloud
expertpro.cloud/choclate to choclate.expertpro.cloud
expertpro.cloud/harmony-in-life to harmony-in-life.expertpro.cloud
expertpro.cloud/paki-cold-places to paki-cold-places.expertpro.cloud
expertpro.cloud/you-are-one to you-are-one.expertpro.cloud
I already have some code for Nginx
The empty location = / block is necessary so that you don't redirect
http://example.com/ to http://.example.com/.
//replacing domain name in rewrite rule
location = / {
# don't redirect $server_name/
}
location / {
rewrite ^\/([^\/]*).*$ https://$1.$server_name/ redirect;
OK, all you need is an internal rewrite, as it looks:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]
You obviously need the rewriting module to be loaded into your http server.
That would be a variant which additionally redirects direct requests to the internal URL:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/?([^/]+)(/.*)?$ https://$1.example.com$2 [R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]
Here it makes sense to start out with a R=302 temporary redirection and only to change that into a R=301 permanent redirection once everything works as expected.
In general you should try to implement such rules in the actual http server's host configuration. If you have no access to that you can instead use a distributed configuration file (".htaccess"), but those come with disadvantages. You need to enable the interpretation of such files in that case.
Problem
I would like to edit the WordPress .htaccess file such that requests to www.mydomain.com/nonwordpress are rewritten to point to a folder outside the WordPress installation.
Details
In Detail: The WordPress installation is on shared hosting and the domain is configured to point to the WordPress installation:
www.mydomain.com -> /absolute_path_on_server/htdocs/wordpress
Now I would like to have a different directory "nonwordpress" outside the "wordpress" folder to be redirected like this:
www.mydomain.com/nonwordpress/ -> /absolute_path_on_server/htdocs/nonwordpress
Note that the other folder is outside the root folder to which the domain points.
What I tried:
I tried modifying the .htaccess file in the folder "wordpress" by adding the following line for rewriting requests to mydomain.com/nonwordpress:
RewriteRule ^/nonwordpress/(.*)$ /absolute_path_on_server/htdocs/nonwordpress/$1
The entire .htaccess file now looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/nonwordpress/(.*)$ /absolute_path_on_server/htdocs/nonwordpress/$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Unfortunately it has no effect, so when I go to mydomain.com/nonwordpress I get a 404 site not found error (from WordPress) instead of the folder contents.
I also tried to achieve the rewriting by replacing the absolute path by a relative path:
RewriteRule ^/nonwordpress/(.*)$ ../nonwordpress/$1
Again, the same error.
Any idea how to do this, if it can be done at all, will be appreciated!
Why this doesn't work the way you expect, see RewriteRule
The Substitution of a rewrite rule is the string that replaces the original URL-path that was matched by Pattern. The Substitution may be a:
file-system path
Designates the location on the file-system of the resource to be delivered to the client. Substitutions are only treated as a file-system path when the rule is configured in server (virtualhost) context and the first component of the path in the substitution exists in the file-system
URL-path
...
So this cannot work, when you put it in a .htaccess file. To have the desired effect, you need to put it in the main configuration wrapped inside a VirtualHost directive.
Although in your case, it would be easier to utilize an Alias
Alias /nonwordpress/ /absolute_path_on_server/htdocs/nonwordpress/
but again, this is also only allowed in
Context: server config, virtual host, directory
My progect structure
/root_dir
/app
/bootstrap
...
/public
index.php
.htaccess
/wp #wordpress installation here
/wp-admin
/wp-content
/wp-includes
index.php
.htaccess
...
So what I want to acomplish is to have Laravel project with views, API etc
And I need wordpress only for one route domain.com/blog where I'll have separate default WP blog that doesn't integrate or interact with my Laravel app in any way
My /public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !^(blog) #ADDED ONLY THIS MAGIC LINE
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
And my /public/blog/.htaccess is WP default
And it works like a charm!
My lara routes are fine... API routes - good... WP blog - great...
BUT
Now I want to call WP API, e.g. domain.com/blog/wp-json/wp/v2/posts
And all I see is Laravel's "NOT FOUND" page
Please help!
I have a strong feeling it can be solved with .htacess 'magic' but unfortunately I'm noob in that...
P.S. Found a similar question here Laravel project next to Wordpress project (in public_html folder)
But I need vice versa solution
you can proxy pass the address http://example.com/blog to some other address,
for example using nginx :
server {
listen 80;
server_name blog.example.com;
location / {
root /var/www/wordpress;
try_files $uri /index.html;
}
}
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/laravel;
try_files $uri /index.html;
}
location /blog/ {
proxy_pass http://blog.example.com/;
}
}
This is a simple and also clean way, and also you don't need to change even one line of code to have laravel and wordpress aside each other.
Holy cow!
The solution was to navigate to dashboard/settings/permalinks and change option from 'plain' to any other!
Found the answer here https://wordpress.org/support/topic/rest-api-in-wp-4-7/#post-8620246
You need to be using pretty permalinks to access /wp-json/. If you’re not, you can instead use ?rest_route=/ to get the index and ?rest_route=/wp/v2/posts (e.g.) for specific routes.
We have a server where nginx is installed, we have also configured PHP As FastCGI on the server. Everything is working fine except rewrite rule. Our requirement is to mask an URL
for eg:- if someone search in our website the URL which comes will be like http://example.com/search.php?searchword=$1 ($1=searched word) . We need to display URL for our customers as http://example.com/$1.html.
We have set rewrite rule as rewrite ^/(([a-zA-Z_0-9]|-)+/?)$ /search.php?searchword=$1 break;
The URL is getting redirected however we get a file not found error each time. How can we mask the URL just as we do in Apache. Any help would be greatly appreciated
Equivalent Apache htaccess rules which we used are as follows
RewriteCond %{REQUEST_URI} !index\.html$ [NC]
RewriteRule ^([a-zA-Z0-9-/]+).html$ search.php?searchword=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ search.php?searchword=$1 [L]
It was working fine with Apache
You should put this rewrite code to location / . I've tested it on my server.
location / {
rewrite ^/([a-zA-Z0-9/-]+).html/?$ /search.php?searchword=$1 last;
}
How could I go about mapping a domain alias, e.g. domainAlias.co.za, to a virtual folder under, e.g. mainDomain.co.za, so that all requests to domainAlias.co.za actually get served by mainDomain.co.za/domainAlias ?
A URL Rewriter like IIRF lets you do this.
The rules would be:
RewriteCond %{HTTP_HOST} ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteRule ^/(.*)$ /%1/$1 [L]
In English, this rule says: if the host is NOT maindomain.co.za, but still ends in .co.za, then rewrite the URL so that it is prepended with /domainAlias/. With this rule, you get:
input output
----- ------
http://foo.co.za/a.php http://main.co.za/foo/a.php
http://foo.co.za/a.aspx?r=1 http://main.co.za/foo/a.aspx?r=1
You can also go one level further and make the rewrite conditional on the presence of the directory, something like this:
RewriteCond %{HTTP_HOST} ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteCond c:\wwwroot\%1 -d
RewriteRule ^/(.*)$ /%1/$1 [L]
This says: if the host is not maindomain.co.za, AND the directory c:\wwwroot\domainAlias exists, then rewrite to prepend ....
But in that case you might instead want to do the converse - test for lack of existence of the directory - and redirect to a 404:
RewriteCond %{HTTP_HOST} ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteCond c:\wwwroot\%1 !-d
RewriteRule ^/(.*)$ - [NF]
NF = 404
you can also do [F] which is a 503 (Forbidden).
IIRF works on IIS5, IIS6, or IIS7.
I haven't used it, but IIS has a URL Rewrite Module that can import Apache mod_rewrite rules. There is also a document that compares IIS URL Rewriting and ASP.NET routing. With some research, you should be able to get that working.
You can use routing.
System.Web.Routing