RewriteRule relative to subdirectory the htaccess is in - apache

I have an site subdirectory, with the .htaccess in it:
www.example.com/projA
/var/www/html/projA/.htaccess
I want to redirect all www.example.com/projA/* to my file at /var/www/html/projA/index.php
I currently have the following, which works:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /projA/index.php [L]
but since my .htaccess file is inside of the projA folder already, is there a way to write this rule so that I don't need to reference projA at all? E.g. if the base path could be relative to .htaccess location, I'd just want to specify this:
RewriteRule . ./index.php [L]
I don't want to reference "projA" because it's a path that may change often.

You can remove reference to projA by putting the htaccess in your projA folder and use relative path in your rewrite rule.
Put your htaccess in /var/www/html/projA/.htaccess
RewriteEngine on
# enable the following line if you want to serve directories directly (without index.php)
#RewriteCond %{REQUEST_FILENAME} !-d
# enable the following line if you have any other files on you want to serve directly
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !=index.php
RewriteRule ^(.*)$ index.php/$1 [NC,QSA]

Related

Point folder root URL to file using .htaccess

In my document root I have a subfolder link such that domain.com/link/something points to domain.com/link/default.php?id=something
In my .htaccess file (located in the subfolder link), I have the following:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT} !-f
RewriteRule ^(.+)$ /default.php?id=$1 [L,QSA]
Unfortunately, it doesn't seem to work as domain.com/link/something gives a 404 error. Any tips?
With your shown samples please try following .htaccess rules file.
Make sure:
Keep your .htaccess rules file, default.php files along with your link folder.
clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /link/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ default.php?id=$1 [QSA,L]

RewriteCondition in .htaccess except for one folder?

I would like to do the following:
I have a htaccess file that makes every request go trough index.php. If I am not mistaken this is called bootstrapping? (I haven't done this before).
I would like to make a subdirectory in the root directory of the site that will serve as a "test" site because I cannot add a subdomain. I need a htaccess rewritecond that will redirect any request under the teszt folder to the index.php in the same folder.
So if I enter example.com/[anything] I get data sent to the index.php in the root and if I enter example.com/teszt/[anything], the data needs to be sent to teszt/index.php
This is my htaccess file:
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You can use these rules in htaccess in your root folder :
RewriteEngine On
#rewrite /subfolder/foobar to /subfolder/index.php
ReweriteRule !subfolder/index\.php /subfolder/index.php [L]
#rewrite /foobar to /index.php
RewriteRule !index\.php /index.php [L]
The rules above also rewrites your existent files to index.php . If you do not want to rewrite your files , just add a condition to the rule RewriteCond %{REQUEST_FILENAME} !-f .

.htaccess to customize php url in folder

My php file is in this location:
https://example.com/store/store.php?id=1
and I want to rewrite it as:
https://example.com/store/store_name
I tried like below:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)/?$ store.php?id=$1 [NC,L] #Handle page requests
But it is not working for me.
With your shown samples, please try following htaccess Rules file. Make sure to place your htaccess Rules file in folder where store folder is present, place it along side with store folder NOT inside it.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/(.*)/?$ store/store.php?id=$1 [QSA,NC,L]

How to .htaccess pseudo-static + hidden extension?

Why can't I use the following?
RewriteRule ^(.*)$ $1.html [L]
.htaccess file:
RewriteRule ^(.*)$ index.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^(.*)$ index.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
The first rule rewrites everything to index.php. The directives that follow are effectively ignored.
However, the second rule also rewrites everything (same pattern ^(.*)$ - obvious conflict), that doesn't map to an existing file or directory to append a .html extension. This second rule is more restrictive.
It seems that what you want to do is:
Append the .html extension to URLs that would map to .html files.
Rewrite all other requests that (I assume) do not map to physical files and directories to index.php.
Additional assumptions
The .htaccess file is located in the document root.
Requested URLs that should map to .html files do not contain dots in the URL-path. Therefore, dots in the URL-path indicate file extensions only.
If you literally rewrite everything else to index.php (as you were doing) then it will also rewrite all your static resources (CSS, JS, images, etc.), so I assume you want to make exceptions for static resources and anything that would otherwise map to a file or directory.
Try the following instead:
RewriteEngine On
# Append the ".html" extension if the target file exists
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^.])$ $1.html [L]
# Rewrite other requests that don't map to files/directories to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(?:css|js|jpg|png|gif)$ index.php [L]

how to remove .php extension from the url

there is no .htaccess file on my server website folder so i paste .htaccess file from other website in my website and write the rule as
RewriteRule ^terms-of-use$ terms-of-use.php [L]
RewriteRule ^privacy-policy$ privacy-policy.php [L]
but does not work.
You could try something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ $1.php [L]
It would make (almost) any of your PHP files to work without the .php extension. The only exclusion is, for example:
If you have a file called index.php, and a directory called index, then it would prefer the directory first (as the RewriteCond %{REQUEST_FILENAME} !-d tells the rewrite engine to only apply this rule, if the target is not a directory).