How do i use htaccess rules to rewrite to a certain file? - .htaccess

I have to create a page handler, which should read the URL, and do specific operations based on querystring.
I'd need to use .htaccess to do some URL rewriting thing to point everything at a certain file which does the processing, in such a fashion:
https://example.com/folder/page1/
https://example.com/folder/page2/
And the processing file is https://example.com/folder/index.php
Is there any way to do that (possibly by removing the index.php part)?

try this out and see if this helps you achieve what you are looking for.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
The above code is taken for reference from WordPress and it should work when the "RewriteBase /folder/" folder name is updated with your folder and .htaccess file has to be placed in the root/folder directory where your index.php file is located.

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).

Modify .htaccess for codeigniter URLS

I am trying to remove the index.php from the url in codeigniter. Rewrite mod scripts are all over the place but I can't find the .htaccess file!! I tried looking in the root directory and everywhere else, but no luck.
From what I read it should be in application folder and when I go there i find the .htaccess file and all it has is deny from all. This is not the same content every one else is sharing online before modification.
Please advise.
Actually you don't find it; you create it along side with your index.php file with this contents:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php,
images, and robots.txt is treated as a request for your index.php
file.
Reference.
The solution is as follows ( for future references)
Create the .htaccess file in the root of the codeigniter application (where you got system, application, etc folders).
Paste this code in it:
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Open config.php and change index = "index.php" to "". Things should work now.

Multiple Rewrite Rules, Try A then B

I have the following directory structure:
root
--/inc
--/img
--/docs
---/public
----/contact
-----/img
------telephone.jpg
-----contact.php
---/private
My aim is to make each folder under 'docs' a 'contained' webpage. Each folder will have it's own /img/ folder, and a /bin/ folder too, which could contain anything from Mp3s to PDFs.
Currently I am routing everything through to index.php, and then manually redirecting the file from there. But this is proving to be very slow. What I was thinking would be faster would be something like this in my .htaccess if say, an image was trying to be accessed via /contact/telephone.png:
try /img/{url_path}
Otherwise, try /docs/$1/img/$2
Otherwise route through index.php
How could I go about doing this? Currently my .htaccess is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# if file not exists
RewriteCond %{REQUEST_FILENAME} !-f
# if dir not exists
RewriteCond %{REQUEST_FILENAME} !-d
# avoid 404s of missing assets in our script
#RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|css|js)$ [NC]
RewriteRule .* index.php [QSA,L]
</IfModule>
Any help appreciated! Thanks
You can do something like this in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# check if this image path exists in docs/<folder>/img first
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/docs/$1/img/$2 -f
RewriteRule ^([^/]+)/(.+?)/?$ /docs/$1/img/$2 [L]

.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.

htaccess help needed

i'm doing maintenance work on a cms and have found the following htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
i'm having trouble understanding it.
the reason a went looking for the htaccess file is this:
i placed some code in index.php (right now just printing some string to a file but
eventually will do banner cycling) and i've noticed the string gets printed a few times when i load index.php. could that have some connection to the htaccess file?
thanx in advance for any input.
This simply checks whether a file exists (as a file -f, or directory -d). If it does not, it takes the address and passes it to index.php.
For instance if you ask for:
www.mysite.com/badfile.html
You will get:
www.mysite.com/index.php/badfile.html
This should have no effect on how the code in index.php runs. This only affects what happens when non-existent files and directories are requested.
Request is for a file on the webserver, denies access to index.php
RewriteCond %{REQUEST_FILENAME} !-f
Request is for a physical directory on the webserver, denies access to index.php
RewriteCond %{REQUEST_FILENAME} !-d
Any other than the above, redirects to index.php
RewriteRule ^(.*)$ index.php/$1 [L]

Resources