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.
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.
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.
I've just started using Nginx instead of apache.
I'm trying to find a similar way to do something like this, in Apache:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
But in Nginx.
So basically any .co.uk traffic gets redirected to .com
You could create a separate server block for the .co.uk domain:
server {
listen 80;
server_name .example.co.uk;
return 301 $scheme://www.example.com$request_uri;
}
See this document for details. Note that the default_server option can also be used to match any non-specific domain name.
I’m having a problem since changing my permalinks from default to postname on a wordpress site. I’m getting 404 errors on pages but only the ones that are within the submenu called menus, all the other pages are working fine.
I have been searching on and off for an answer to this for a couple of days now with no success. Someone suggested that it might be an error in the HTaccess file but I don't really know what I'm looking at. I tried generating a new one from Wordpress.org code I found but that just killed the site altogether so I changed it back to the old one. I can see the .htaccess inside the folder public_html there are some others in there with .bak which I assume are from backups I've made of the site. Here is a link to what I'm getting http://www.5thview.com/menus/nibbles/
Not sure why it’s just those pages that are affected other than they are in a submenu. I’ve no idea how to fix this.
I should add that if I switch back to default permalinks it works fine again…
I would need specific instructions if poss as I'm a bit of a newb to the coding side etc although I can find my way around if directed...
Any help would be greatly appreciated. Thanks...
The reason links are broken is that your host doesn't have root directive correctly set.
If you are using Nginx, change the virtual host conf file as below:
server{
location / {
try_files $uri $uri/ /index.php?q=$uri$args;
}
}
If your root wordpress is not the webroot but http://[domain.com]/wordpress/:
server{
location /wordpress/ {
try_files $uri $uri/ /wordpress/index.php?q=$uri$args;
}
}
Don't forget to restart nginx service.
If you are using Apache, put .htaccess file under your root path, an add below lines
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Don't forget to restart apache service.
tl; dr
I have a Magento install on www.example.com
I have a Wordpress install on www.example.com/wordpress
And I need the following urls to also serve that same wordpress content;
www.example.com/eu/wordpress
www.example.com/gb/wordpress
P.S.: I know there are duplicate content issues with this, please ignore that
The question is: What's the best way to do that?
The full story
I have a Magento multi store site using the 2 digit language code subdirectory technique.
I have one Wordpress installation in it's own subdirectory.
app
downloader
errors
eu/ - symlinks for the € Euro store
gb/ - symlinks for the £ UK store
includes
js
lib
media
shell
wordpress/ - The Wordpress install
var
I need the Wordpress blog to be available from all stores so they user stays in the store with their locale/currency.
What I have Tried
Using the answers in these Stacks;
htaccess multi language site with sub directories, and default 301
Endless Redirect Loop by htaccess rules multi language
I've made attempts but unfortunately I am terrible with .htaccess and vhosts problems
Via the vhosts file
<VirtualHost *:80>
ServerName www.example.com/eu/wordpress/
ServerAlias www.example.com/wordpress/
DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com/gb/wordpress/
ServerAlias www.example.com/wordpress/
DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>
Via the Wordpress htaccess
RewriteCond %{REQUEST_URI} !^/(eu|gb )/wordpress(/|$) [NC]
RewriteRule ^(.*)$ wordpress/$1 [R=301,L]
Via the Magento .htaccess
RewriteCond %{REQUEST_URI} ^/eu/wordpress/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/gb/wordpress/(.*)$ [OR]
RewriteRule ^/wordpress/.*$ - [L]
Firstly, you don't need vhosts for wordpress, you need only one vhost per domain and/or subdomains. Your vhost should look something like this(which I assume that you already have a vhost similar to this for your magento shop):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/vhosts/www.example.com/public
<Directory "/var/www/vhosts/www.example.com/public">
# allow .htaccess files to override all directives
AllowOverride All
</Directory>
</VirtualHost>
Now, you just need to modify magento's .htaccess(/var/www/vhosts/www.example.com/public/.htaccess), and add the following rules:
<IfModule mod_rewrite.c>
RewriteEngine on
# rewrite rule to redirect
# eu/wordpress -> /wordpress/
# eu/wordpress/ -> /wordpress/
# gb/wordpress -> /wordpress/
# gb/wordpress/ -> /wordpress/
RewriteRule ^(eu|gb)/wordpress/?$ /wordpress/ [R=301,NC,L]
# ... continue here with magento's rewrite rules ...
</IfModule>
We have this in production, hopefully this answer might help someone at some point.
The way we've done this is to add a RUN_CODE environmental variable which is used in a custom Wordpress filter to get the urls all working. I've used the 'eu' example from my question to illustrate it below. Note we had to do this for both Apache and Nginx so I've added the .htaccess and server blocks for both.
APACHE - .htaccess
In the eu country stub subdirectory you add this to your .htaccess file (so eu/.htaccess)
RewriteCond %{REQUEST_URI} ^/eu/wordpress$
RewriteRule ^(.*)$ /eu/wordpress/ [R=301]
RewriteCond %{REQUEST_URI} ^/eu/wordpress(.*)$
RewriteRule ^(.*)$ /wordpress/index.php/$1 [L,E=RUN_CODE:eu]
NGINX - Server Block
location ~* /eu/wordpress(.*) {
if (!-f $request_filename) {
set $code 'eu';
rewrite ^(.*)$ /wordpress/index.php?$1 last;
break;
}
}
In the Wordpress root index.php this was added at the top of the file
$pos = strpos($_SERVER['REQUEST_URI'], '/wordpress');
if ($pos !== 0) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], $pos);
}
Wordpress theme functions.php
add_filter('post_link', 'link_mcnab');
add_filter('page_link', 'link_mcnab');
add_filter('bloginfo_url', 'link_mcnab');
function link_mcnab($link)
{
if (isset($_SERVER['REDIRECT_RUN_CODE']) && $_SERVER['REDIRECT_RUN_CODE']) {
$homeUrl = home_url();
$domain = substr($homeUrl, 0, strrpos($homeUrl, '/'));
$link = str_replace($domain, $domain . '/' . $_SERVER['REDIRECT_RUN_CODE'], $link);
}
return $link;
}