I'm using Wordpress and have the following pemalink /%category%/%postname%.
This gives me user friendly URLs like http://www.example.com/something/.
With this permalink, I will not be able to access php files directly, e.g. http://www.example.com/something/myfile.php.
I need to write a ReWrite rule which allows me access to /include/myfile.php.
Can some help me please? :)
Here's my current .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myblogdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /myblogdirectory/index.php [L]
</IfModule>
# END WordPress
Update
Ok, I got it working.
I was doing it wrong from the start. I was using the "virtual" path instead of the physical path.
Instead of /mysite/includes/myfile.php, I have to use /wp-content/themes/mytheme/include/myfile.php
I could probably also have added RewriteCond %{REQUEST_URI} !myfile.php, which would have excluded myfile.php from the rewrite rules. I have not tested this though.
Well, your rewrite rules looks good.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
means that /blog/index.php won't be serverd if %{REQUEST_FILENAME] is a physical file or directory.
More info here.
Are you sure that you're using the correct file paths?
The file you want to request should be located in /blog/include/myfile.php.
Check your error.log for apache for any related messages.
Related
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.
I have installed WordPress in example.com. I have also installed CodeIgniter in ci folder here example.com/ci/, ci is the folder name , register is the controller name and working URL of CI is example.com/ci/register . My base URL starts with https:// .
Now I have one WordPress URL example.com/hotel, hotel is the page that I have created in WordPress admin, it works fine.
I want to run my CI path like example.com/hotel/ci/register, I think we can do it with some rewrite rule so that my URL would look like example.com/hotel/ci/register. I have added given htaccess for wordpress that redirecting me here example.com/hotel/ci/register. It is showing me 404 error of CI. It means now I am in CI. Now I did following things in routes.php file.
$route['default_controller'] = 'register';
$route['404_override'] = 'register';
Now this URL example.com/hotel/ci/register is working, but this is not right way, next time there will be more controllers then it will not work.
Note: I can not create hotel folder because hotel is a page in the WordPress. If I create hotel folder then WordPress URL example.com/hotel/ will not work. It will redirect WordPress page to the hotel folder. So I have to do it without creating hotel folder. Note example.com=myurl.com .
I need to find another good solution.Any advise or guidance would be greatly appreciated?
Following is my reWrite rule in wordpress htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/?hotel/ci/register(/.*)?$ /ci/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And following is my CI htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
Interesting problem! I set up a Docker container with a fresh install of Wordpress and Codeigniter, I created a hotel page in WP, and a Register controller and view in CI, and got testing. I spent way too long on this, but I did find an answer.
First, your Wordpress .htaccess. As #tobiv pointed out in a comment, you should not add anything between the BEGIN/END WordPress comments as it might get whacked by a WP update. Your redirect has to come before the standard WP rules though, so add it at the top of the file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^hotel/ci/register /ci/register [L]
</IfModule>
# BEGIN WordPress
# ... These are the default, unchnaged WP rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c> and RewriteEngine On are duplicated which seems messy but you do need them, as your new rule has to go first so it processes the request before the WP rules do.
You don't need to modify the Codeigniter .htaccess file, the default one is all you need. It should be in the root of your Codeigniter installation, ci/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
At this point https://example.com/hotel/ci/register will show the Codeigniter 404 page. If you have logging enabled (see config/config.php), and you watch the logfile, you'll see why:
ERROR - 2017-11-14 17:57:20 --> 404 Page Not Found: Hotel/ci
And here's the root of the whole problem. Since the initial redirect is an internal redirect (means the URL shown in the browser does not change), the URI Codeigniter receives to process is the one still shown in the browser address bar - hotel/ci/register. Codeigniter will try to handle a request like that in 2 ways:
Look for a matching route in application/config/routes.php
Or look for a controller named application/controllers/Hotel.php, with a method called ci;
In our case there is no Hotel controller, and no route to describe how to handle such a request, so boom, 404.
One simple solution is to create a route to handle this request:
$route['hotel/ci/register'] = 'register/index';
And now https://example.com/hotel/ci/register works!
Notes:
Setting your default route to register ($route['default_controller'] = 'register';) means that https://example.com/ci/ will also show register. I'm not sure if you want that? You might run into duplicate-content SEO problems if that URL shows the same as https://example.com/hotel/ci/register, so maybe you want something else, or a 404, there.
Make sure you remove your $route['404_override'] = 'register'; route;
CI base_url is not relevant for this problem, though obviously should be set. Depending on how you want your links to be I think either http://example.com/ci/ or http://example.com/hotel/ci/ would be right.
I am not quite sure what the purpose of this condition in your CI .htaccess is for:
RewriteCond $1 !^(index\.php|resources|robots\.txt)
The existing default conditions already skip files and directories which exist on disk. That's what the !-f and !-d conditions mean - "if the requested pattern does not match a file or directory on disk, then do the redirect".
If you have a robots.txt file on disk (in your ci/ dir), and someone requests https://example.com/ci/robots.txt, the !-f condition will fail, and the rewrite is skipped - meaning the request is handled without rewrites and robots.txt is returned successfully. Same for index.php. If you have a directory called ci/resources, and someone requests https://example.com/ci/resources, the !-d condition will fail, the redirect is skipped, and the request is successfull.
I'm not sure about your resources part, but maybe you can remove that condition completely.
If you don't need pretty Codeigniter URLs (I mean other than https://example.com/hotel/ci/register, this change won't affect it), and it turns out you don't need that extra condition above, you can get rid of the CI .htaccess completely to simplify things. To do that, just change the Wordpress RewriteRule to the non-pretty version:
RewriteRule ^hotel/ci/register /ci/index.php/register [L]
And delete your CI .htacces.
I think you are on the right path, try this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/hotel/ci(/.*)?$ /ci/$1 [L] # remove the register part
# so that it would be handled by CI
# also remove the "?" in front of hotel
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
keep your CI htaccess as it is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
the last part is match your CI config.php to your new url model
$config['base_url'] = "http://example.com/hotel/ci/";
I don't thing this will be the great answer of your question but if i face the same problem i will use routes see the example code.
$route['(:any)/ci/register'] = "register";
what will the above code do (:any) means any word in first uri and after that you can defined any url you want i defined ci/register you can also do that like this.
$route['(:any)/register'] = "here_you_can_add_your_controller/function";
this will work if hit url like this.
http://www.example.com/any_word_you_want/register
it will hit your controller function. you need to echo something and it will show in your browser.
you can also defined the hotel word in your base url as #am05mhz shows in his answer but i don't thing that's a great idea because in future may you have 2 more words in your url.
Note : the above code example only work if your .htaccess give access of routs as you shows in your question the .htaccess is work for you. For full knowledge of routs please check the documentation of codeigniter URI routing
Use htaccess [P] flag instead.
Make sure the substitute (target) string will start with http://. Otherwise, it will treat the substitute string as an internal file path.
Check the code below.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/?hotel/ci/(.*)?$ http://example.com/ci/$1 [P]
# If you want rewrite any URI as long it has */ci/* in it.
# Use the following rule instead.
# RewriteRule ^(.*)/ci/(.*)?$ http://example.com/ci/$1 [P]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Also, you don't need to update CI's base url. Empty string will do.
$config['base_url'] = '';
Hope that helps.
Links:
https://httpd.apache.org/docs/current/rewrite/flags.html#flag_p
Hello guys what can be the reason for this:
I have a folder "wallpapers" not related to my website (Wordpress) in the same web hosting and images inside. If i wanted to access them I would go to website.com/wallpapers/myimage.jpg and it worked!
But I noticed it doesn't work anymore now I see my wordpress site + error 404 inside the website.
I have tried to fix this disabled few plugins etc... but where should I look? What can be the reason? Maybe the htaccess?
Thanks!
Yes, I think your guess on the .htaccess is perfectly correct. If you enable permalinks in Wordpress it automatically creates, or tries to create (depending on permissions) the following .htaccess file;
# 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
The two lines in this that are relevant to you are;
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
What they are basically doing is making sure that every url request gets run through the index.php in the root except any files or directories that actually exist as separate files and directories outwith the Wordpress install (ie your wallpapers directory). So first of all I would make sure that your .htaccess looks like this.
I needed to do the same in the past, and it drove me nuts so if that can help here's what I've done. I had the default .htaccess wordpress created in my wordpress folder when activating the permalinks option, the folloowing :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
for some reason...
I could access http://example.com/phpinfo.php fine,
but http://example.com/myfolder was returning a 404, despite (if I'm correct) the line RewriteCond %{REQUEST_FILENAME} !-d should allow me to show what was into that folder as it's existing and having the correct permissions (user:user chmoded 755)
after having tried everything I could find on the subject, I ended up creating a .htaccess into my folder "/myfolder"
with the unique following line in it :
Options +Indexes
And I finally got http://example.com/myfolder to answer the response 200 OK
I suppose this is not the best ever solution but it's the only one that worked for me and as I just need this to work for one or two folders, it did not need to be more adaptive / flexible
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".
This is my WP htaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I want wordpress' htaccess to stay out of the way when the link is to a standalone .html|.htm file inside the /ag directory.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/ag
RewriteRule . /index.php [L]
Doesn't work (I am aware the above code doesn't even try to implement the requirement that the file ends in .html|.htm). What do I need?
The original rewrite conditions only rewrite if the file does not exist, and is not a directory.
Therefore if you have the file in a directory, there should be no rewrite happening.
Wrong answers here. This thread helps:
non-wordpress files in wordpress installation directory
You need to specifically exclude the directory using rewrite rules, or Wordpress 3.4.1 won't allow access.