URL rewriting - Remove a subfolder from the url - wordpress

I don't often ask questions on SOF because I usually find the answers just searching on SOF. This time it's a tough one for me! I don't have a lot of experience with htaccess. In fact I don't have much experience, 6 months out of school now!
I am working on a personal project using Wordpress 4.9.8 and Laravel 5.7.
Wordpress will is used as the entry point and the articles part, Laravel will be used for the app part to display large amount of info from database.
I am developing locally with Xampp and MySql Workbench.
I am using a virtual host to simulate the domain name.
The project is structured like so in the root folder:
Wordpress folders and files
htaccess
Laravel folder
all folders and files from Laravel
public
htaccess
The project URL is like so:
dev.domain.com/ -->Wordpress part
dev.domain.com/plants/public --> Laravel part
I need to hide the "public" folder of the URL only and still access that directory.
So far I was able to redirect directly skipping the root folder of Laravel using htaccess.
dev.domain.com/plants to dev.domain.com/plants/public
I can't seem to put the finger on it after trying everything that I found that makes sense and not sure exactly where to put it. At the root of the project or in the public directory for this part?
Here is what I have in the root of the project:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^/plantes [NC]
RewriteRule . plantes/public [L]
</IfModule>
What I have in the public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
-- Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
-- Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
-- Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Thanks for any help or tips!

After trying some solution found on SOF and elsewhere without any success, I turned around to review the way I structured the project to see if there is other simpler way to do this without htaccess.
Here is the new structure:
Wordpress folders and files
htaccess (no particular rules here so far except the ones form WP)
MyApp
Laravel folder (containing all folders and files from Laravel except the public folder)
The content of the public folder
For this to work we need to change some references in the index.php that was in the public folder to reflect the new structure.
Index.php:
require DIR . '/Laravel/vendor/autoload.php';
$app = require_once DIR . '/Laravel/bootstrap/app.php';
That it's... It works!
www.domain.com/myapp/what-ever...
No URL with the public folder in the way.
Now I will need to work to change the name of "MyApp" so it can reflect multi languages using htaccess.
During my search I found this very helpfull: https://www.youtube.com/watch?v=ybJYyU5FPv4

Related

.htaccess : how make URL rewrite for Symfony AND Wordpress at the same time

i have one domain and one server for all my web app, my server already got a wordpress and a php app but i want to add a symfony app on it (i really understand that's not a good way to do it).
But i have troubles with my .htaccess...
My server looks like :
server_root
My wordpress works correctly, myapp1 (full php) works correctly, but myapp2 (Symfony 4) doesn't seems to work.
When i go to www.mydomain.com/myapp2/public i have the good redirection to www.mydomain.com/myapp2/public/login but with a wordpress 404 not found error ...
i'm 100% sure i have to change something in the .htaccess file but i can't figure out how making it work...
i tried to add some lines but nothing seems to work..
here is the .htaccess file of my server :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
i really need help on this ... sorry to bother with my problems,
thanks in advance
As Jenne said your current .htaccess for your Wordpress application does rewrite everything which is not
an existing directory
an existing file
to the index.php of Wordpress.
That is why your PHP application has worked fine. Symfony however needs a rewrite of everyting to its index.php too.
I do not recommend locating Symfony inside a public directory. As Jenne said you should locate it outside of it and e.g. create a own subdomain which root directory is the public directory of your Symfony application.
But you can add an extra rewrite rule to your .htaccess file which does the needed rewriting for Symfony.
# If the path points inside the myapp2 public folder
RewriteCond %{REQUEST_URI} ^/myapp2/public/.
# and there is a file at that path
RewriteCond %{REQUEST_FILENAME} -f
# use that file.
RewriteRule ^ - [L]
# If the url points to /myapp2 use the Symfony index.php.
RewriteRule ^/myapp2(/.*)?$ /myapp2/public/index.php [L]
# If the path points to something which is no existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# use the Wordpress index.php.
RewriteRule . /index.php [L]
That way you can access all files in the Symfony public directory like that:
www.example.com/myapp2/public/somefile.txt
And use everything else at /myapp2 for your Symfony routes. But all files which are in your myapp2 folder and outside of the public folder can't be accessed.
Lets do a warning first: having all these files in those public folders like this is very BAD as we can simply open the configuration files browsing to eg yourhost/myapp1/config/services.yaml. And these will be fully readable! It is therefor advised to only expose the public folder of symfony projects (the one that has the index.php) to the outside world. Or you would have to edit the htaccess even further to only allow acces to specific file types/folders like assets
There is an answer to the question though, what is going on is:
# If the requested path is NOT (!) an actual file (-f) on the disk
RewriteCond %{REQUEST_FILENAME} !-f
# And if the requested path is NOT (!) an actual directory (-d) on the disk
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite any path (`.` is a regex where the dot will match anything) to /index.php
RewriteRule . /index.php [L]
There for calling file/directory in myapp1/2 would work as they are actual file calls and wont get forwarded to the root/index.php (wordpress).
We can solve this by adding
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/myapp1
RewriteCond %{REQUEST_URI} !^/myapp2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The first two can also combined in regex using RewriteCond %{REQUEST_URI} !^/(myapp1|myapp2)
This results in any path:
NOT starting with /myapp1
AND NOT starting with /myapp2
AND IS NOT a file
AND IS NOT a directory
being rewritten to /index.php.
You can then also add a .htaccess in myapp1/2 to do the rewriting for those apps specifically.

Apache access to example.com/app from different root directory

For now, I have following directories:
WordPress directory is connected with example.com domain. I would like to access my Zend app by example.com/app URL, but it is in separated directory. I tried to add rewrite rule in htaccess file in WordPress directory, but server responds me with 400 error. I tried that without success:
RewriteRule ^app/(.*)$ ../zend_app/$1
And now I am not sure what to do. I think about some php file to include something from zend_app folder or to create symlink inside WordPress directory pointing to zend_app folder. What solution is better? Maybe there is another solution? I am using shared hosting, so possibilities are limited.
EDIT:
Here is my whole htaccess file from WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^app/(.*)$ zend_app/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Laravel pointing subfolder into other name

I have a laravel project that include multiple domain inside.
I want to make the blog for each domain using wordpress. I already install the wordpress succesfully, but since this laravel project is a multiple domain inside, I install the wordpress project for each domain using "domainname_blog" on the public folder.
public_html
|- public
|- aa_blog
|- bb_blog
|- cc_blog
What I want to ask is, is it possible to access the blog using aa.com/blog instead of aa.com/aa_blog?
I hope I can access aa.com/blog that pointing to aa_blog without any redirecting, because now I only can access it using aa.com/aa_blog. I tried a lot of changing htaccess method but still not working.
Please for your advice.
Thanks
You need to segregate the Blog Folder from your laravel .htaccess file. Please write the code into your .htaccess file [ laravel ]. Hope it will solve your issue.
RewriteCond $1 !^(blog)
.htaccess file will look like :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond $1 !^(blog)
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

URL rewriting not working with WordPress

I want to change URLs in one folder (cartoon) in my site to friendly SEO URLs.
This folder (cartoon) includes a PHP script not related to WordPress.
From:
example.com/cartoon/index.php?v=TitleEpisode
To:
example.com/cartoon/TitleEpisode
I read here all related questions but I did not benefit.
I have WordPress on my main domain (example.com).
I found this code in .htaccess file:
DirectoryIndex index.html index.php index.htm parking-page.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
What do I do?
Ok, based on the edit, it seems like what you want is for WordPress not to rewrite that slug but to ignore it.
You can do this by editing your .htaccess to exclude a folder. Make sure the folder is in the root directory of your site, as in, the same folder as wp-admin, wp-content, and wp-includes.
Then, open your .htaccess and add a rewrite rule to ignore that folder:
DirectoryIndex index.html index.php index.htm parking-page.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# Add this condition with the folder you want to ignore (cartoon in your example)
RewriteCond %{REQUEST_URI} !^/(cartoon|cartoon/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
OLD ANSWER
Well, this is fairly open-ended. For a better answer, please edit the question to include what you have already tried. Also, what do you mean by "one folder"? Is this a custom post type? Is it a category or custom taxonomy? Are there different permalinks for different types/taxonomies? Please provide more details on what you want to do.
But for the usual case, here is the documentation for pretty permalinks.
First, make sure URL Rewriting is enabled in Apache. Example in Ubuntu/Debian
sudo a2enmod rewrite
sudo service apache2 restart
Now, in wp-admin, go to Settings -> Permalinks. Set the permalink to Post Name.
Then click save changes. It will either save the new .htaccess automatically if your site has the permissions to, or it will give you the new content of .htaccess to copy and paste.
Now you should be able to view a post or page and it should show the SEO-friendly links.
How are you implementing (or intending to implement) the routing of example.com/cartoon/TitleEpisode?
If this is entirely outside of WordPress then I would expect you to have an additional .htaccess file inside the /cartoon subdirectory (since this is presumably a physical subdirectory)? This alone should be sufficient to override the WordPress mod_rewrite directives in the parent .htaccess file, since mod_rewrite directives are not inherited by default.
For instance, simply enabling the RewriteEngine in a subdirectory is sufficient to override the WP directives.
In /cartoon/.htaccess:
RewriteEngine On
Then, in order to route a URL of the form /cartoon/TitleEpisode to /cartoon/index.php?v=TitleEpisode, you would need something like:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+) index.php?v=$1 [L]
(A bit similar to the WP directives in the parent .htaccess file.)
I would avoid editing between the # BEGIN and # END WordPress markers in the parent .htaccess file since these could be overridden by future WP updates.
You would instead implement an exception before the WP directives. For example:
RewriteRule ^cartoon - [L]
However, as mentioned above, you are probably better off creating an additional .htaccess file in the subdirectory and avoid touching the WordPress installation at all.
it's done.
i enter this after the first line in .htaccess
# BEGIN for Cartoon Folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?cartoon/(.+)$ /cartoon/?v=$1 [L,QSA]
# END for Cartoon Folder
thank u very much

.htaccess with wordpress and drupal site

In my root wordpress site running
I have the usual wp-content, wp-includes, wp-admin folders, but I also have my 'drupal' which is a drupal site
I have .htaccess file in both root and drupal directory
When i try to access any node of drupal website give page not found error of wordpress
Please a help me for that
Add RewriteRule ^drupal - [L] above the WP rules in the .htaccess that is located in the root.
I have seen this a lot with Cpanel and Fantastico.
https://www.drupal.org/forum/support/post-installation/2010-08-09/installing-wordpress-in-a-sub-directory-of-drupal
The topic is old but still relevant. Just wanted to share my solution to this problem.
In WP, go to Admin > Settings > Permalinks;
Scroll down to the
bottom; You should see this:
If your .htaccess file were writable, we could do this automatically, but it isn’t so these are the mod_rewrite rules you should have in your .htaccess file. Click in the field and press Ctrl+a to select all.
Add this to WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /YOURSUBDIR/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /YOURSUBDIR/index.php [L]
</IfModule>
Open your .htaccess in the Drupal root dir, and find this bit:
# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
Paste the WP Admin code underneath this, from "RewriteBase" to the end;
It should work now!
Make sure your WordPress permalinks are reset by hitting save ( the first URL here walks you through it
- https://www.cloudways.com/blog/fix-404-error-on-wordpress/
If that will not work try this.
See:
https://www.drupal.org/forum/support/installing-drupal/2007-08-02/the-requested-url-not-found-on-this-server
- I hope this is helpful cloudways.com will know the fix too.
Add this to WordPress
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Ignoring Subfolders that exist in the DocumentRoot
With clean URL's enabled, when running other software applications in subfolders (subdirectories) of a Drupal root installation. your .htaccess file may rewrite those URL's to Drupal. This may be a particular problem for those with a Drupal installation in the root of their domain using Cpanel and Fantastico where Fantastico installs other software into subfolders. For example, phpSurveyor's admin interface as installed by Fantastico will not work with Drupal's default .htaccess settings. The URL for the admin interface is inaccessible and will return a "page not found" page in your Drupal site.
The trick is to modify .htaccess to ignore specific files/folders. So for example, if you have two folders, and in the root of your Drupal installation, modify your .htaccess file by inserting the following code directly after the "RewriteEngine on" directive, before the Drupal rewrites:
=========[ start of .htaccess snippet]==========
<IfModule mod_rewrite.c>
RewriteEngine on
#
# stuff to let through (ignore)
RewriteCond %{REQUEST_URI} "/folder1/" [OR]
RewriteCond %{REQUEST_URI} "/folder2/"
RewriteRule (.*) $1 [L]
#
====================[ end ]=====================
For each folder, you want to bypass, add a RewriteCond line, and end all but the final RewriteCond with [OR]. Note that the [L] in the rewrite rule tells it to stop there and bypass the rest of the rewrite rules.
Ignoring subfolders that are included via Apache Alias directives
As of 4.7, files and directories should be automatically allowed through in drupal's .htaccess setup. Thats what the !-f and !-d lines do.
However if you are working with Apache Alias or similar directives the file doesn't actually exist so drupal will take over like it should. The best way around it is to just add one more conditional that matches your location and make it skip it too. Thats what the ! means. Please see below:
RewriteCond %{REQUEST_URI} !^/yourDirectoryName
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It essentially means Apply this rule if the REQUEST_URI doesn't start with /yourDirectoryName and the REQUEST_FILENAME isn't a real file or a real folder. Which is exactly what you want. There is an implied "AND" between the lines of that rule. The ! says "not like this".

Resources