.htaccess - Read all sub-directories and files by one file - .htaccess

I am trying to add a .htaccess file into domain.com/links/ so if anyone goes to domain.com/links/mylink/ or domain.com/links/mylink it is passed to domain.com/links/link.php?link=xxxxx but I just can't seem to find the RewriteRule that doesn't keep repeating itself.
RewriteRule ^(.*)$ /links/link.php?link=$1 [L]
The above does the following
domain.com/links/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/something

<IfModule mod_rewrite.c>
RewriteEngine On
# If file exists, don't rewrite
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite everything to link.php
RewriteRule . /links/link.php [L]
</IfModule>

Related

Yii2 app in a subdirectory of a Wordpress website: cannot enable pretty urls

I’m trying to deploy a basic webapp on a shared environment where Wordpress is on the root. The Yii2 app is in /subfolder.
I’m following this guide. In root’s .htaccess I added:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ /web/css/$1 [L]
RewriteRule ^js/(.*)$ /web/js/$1 [L]
RewriteRule ^images/(.*)$ /web/images/$1 [L]
RewriteRule (.*) /web/$1 [L]
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation. It’s the first block of rules capturing all the pages, but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs. I checked mod_rewrite docs but couldn’t understand what’s wrong. Any help is appreciated. Thanks
RewriteBase /subfolder
You cannot set multiple RewriteBase directives in the same .htaccess file. The last instance "wins" and controls the entire file. So, in the .htaccess file you posted, RewriteBase / set in the WordPress code block, is what is actually set for the file.
However, none of the directives actually make use of the RewriteBase directive anyway - so none of the RewriteBase directives are actually doing anything. The RewriteBase directive only applies where you have set a relative path (not starting with a slash) in the RewriteRule substitution string.
but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs.
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
Presumably it's these two RewriteCond directives you are referring to... in which case these two conditions aren't really doing anything. RewriteCond directives only apply to the first RewriteRule directive that follows, so it only applies to the directive that rewrites your assets.
However, this RewriteRule is matching /assets in the document root, not /subfolder/assets, which is presumably the requirement - so these rules will fail to match.
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation.
The rules will certainly "break the blog installation", however, they don't appear to get as far as handling the request "through Yii". There's nothing that actually rewrites the request to /subfolder. However, the following directive unconditionally rewrites everything to the /web directory in the document root (which presumably does not exist) - so this will certainly "break" all the WordPress URLs.
RewriteRule (.*) /web/$1 [L]
In fact, I would have expected this to have created a rewrite-loop (500 Internal Server Error response)?! Unless you have a subdirectory /web off the document root which also contains an .htaccess file containing mod_rewrite directives? But that seems unlikely, since the /web directory should be inside the /subfolder directory?
Try the following instead:
RewriteEngine On
RewriteRule ^(subfolder)/(assets|css|js|images)/(.*) $1/web/$2/$3 [L]
RewriteRule ^(subfolder)/((?!web).*) $1/web/$2 [L]
RewriteRule ^subfolder/web/index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(subfolder)/. $1/web/index.php [L]
# BEGIN WordPress
:
No need for the <IfModule> wrapper. Or the RewriteBase directive.
Alternatively
However, it would be preferable to move these directives into their own .htaccess file in the root of the project, ie. /subfolder/.htaccess - which I believe is what the linked "guide" is suggesting. This keeps the two projects entirely separate. And avoids having to explicitly include the /subfolder in the directives.
In addition, creating a another .htaccess file in the web subdirectory, ie. /subfolder/web/.htaccess. This is again, suggested in the linked "guide". However, this also negates the need for the additional directives to route the request in the parent .htaccess file.
For example, putting these changes together, the /.htaccess file in the document root should only have the WordPress directives. And then...
/subfolder/.htaccess
RewriteEngine On
RewriteRule ^((?:assets|css|js|images)/.*) web/$1 [L]
RewriteRule ^((?!web).*) web/$1 [L]
/subfolder/web/.htaccess
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Again, no need for the RewriteBase directive here - in fact, using RewriteBase here arguably complicates things. When in the /subfolder/web/.htaccess file, all relative URL-paths are relative to that directory.
So, requesting /subfolder/foo gets internally rewritten by the /subfolder/.htaccess file to /subfolder/web/foo. Which is then caught by the /subfolder/web/.htaccess file (preventing a rewrite loop) and internally rewritten to /subfolder/web/index.php (providing foo does not exist as a physical file).

.htaccess change parameters received directly after domain

I have been looking for many tutorials bu no one has really helped me, I know you guys can:
My website has the following structure:
mywebsite.domain/?pg=somepage
But I would like to leave it like this:
mywebsite.domain/somepage
All the tutorials I have found work in the next structure:
mywebsite.domain/file.php?pg=somepage
So I can't gess how a RewriteRule would be using my structure.
I have tried:
RewriteEngine on
RewriteRule ^index/([0-9a-zA-Z]+) index.php/?pg=$1
And also:
RewriteEngine on
RewriteRule ^index/([0-9a-zA-Z]+) index.php?pg=$1
But I have had no luck :(
Note: My index file has .php extension
You could do something like this -
RewriteRule ^(.*)$ /index.php?pg=$1 [L]
This will redirect mywebsite.domain/somepage to mywebsite.domain/index.php?pg=somepage
or full config -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [L]
</IfModule>
for more info - Apache mod_rewrite

Codeigniter .htaccess not ignoring directories

I have a website built on codeigniter and am trying to get the PP IPN working with it. I have looked at a few CI specific libraries, but want to have a stand alone IPN file.
Basically, I have CI installed in the website root and I have another directory 'pp', also in the root.
I'm trying to test the IPN using Paypal's sandbox, but it's returning a 404, I also can't access the file directly.
Here is my .htaccess file:
XSendFile on
Options -Indexes
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
RewriteRule ^(index(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{HTTP_HOST} !^(www) [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|pp|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Here's hoping it's something really obvious!
Thanks,
Ryan
If the pp directory exists, you could just put an htaccess file in that directory and just have:
RewriteEngine On
in it by itself, and any rules in the main htaccess file would get ignored. If your paypal stuff already has rewrite rules in an htaccess file in the pp directory, you may want to look into those rules as the cause of the 404.
Do you have mod_rewrite.c enabled. If not, that .htaccess will always give 404. (Sorry I couldn't make this a comment, don't have the rep).

.htaccess adding new file

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /homepage/index.php [L]
</IfModule>
That is my .htaccess code. I have created new file in homepage/test.php and when i visit it from browser it give error of access,
i have tried by replacing index.php with test.php but it still not working.
Can some one explain me how this code work and why not my code is working.
i have deleted .htaccess but its still not working. What else file it can be.
Change
RewriteRule . /homepage/index.php [L]
to:
RewriteRule (.+) /homepage/index.php?page=$1 [L]
P.S. For this to work, your index.php needs to listen for $_GET['page'] and load the appropriate resource.
EDIT: without using the "?page=$1" your code will just display the index page for all requests that are not files or directories. I am not sure if that is what your were going for.

is if else conditions available on htaccess?

I have a .htaccess file in the folder "services" in my website
and have to give two urls following
content of htaccess
#------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>
$-------------------------
1. http://www.mysite.com/services/seo-work
2. http://www.mysite.com/services/online-editor
The 1st URL rewritten using htaccess like http://www.mysite.com/services/index.php?s=seo-work
This is working correct. but when i am uploading other file online-editor.php on the same folder "services", it is not working
so how the first url should show the page using index.php?s=seo-work if the page "online-editor.php" is not found in directory otherwise online-editor.php shoud show. I am programatically representing my problem below
if(IsFoundPageInDirectory(online-editor.php))
{
GoToUrl(http://www.mysite.com/services/online-editor.php)
}
else
{
GoToUrl(http://www.mysite.com/services/index.php?s=seo-work)
}
Please help me to get out of this problem
Advance thanks for your reply
There is not really if/else conditions with ifModule in Apache configuration, but you can use the test in 2 cases : !module & module, like this:
<IfModule !module>
# default directives
</IfModule>
<IfModule module>
# decorator directives
</IfModule>
Check default wordpress .htaccess file. I think you're missing checks for existing files and directories. Something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Last I found answer.. thanks for the fist reply to lead me to get answer
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>

Resources