Point folder root URL to file using .htaccess - apache

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]

Related

.htaccess Reroute file with hidden parameter to another file

I have a problem which I'm trying to solve nor for hours.
So in my root director i have a folder called plugins with some files in it. The important ones are plugins.php and query.php.
When the url is /test/plugins I wan't to show the content of plugins.php. But when the URL is /test/plugins/param I wan't to keep this URL in the browser but want to show the content of query.php and use param with $_GET['param'] in it. Is this possible?
My latest and most promising .htaccess file looks like this:
RewriteBase /test/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ query.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ query.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ query.php [QSA,NC,L]
With your shown samples and attempts, please try following htaccess rules file.
Make sure to keep your htaccess rules file inside plugins folder and also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^test/plugins/?$ plugins.php [QSA,NC,L]
RewriteRule ^test/plugins/param/?$ query.php [QSA,NC,L]

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

.htaccess to serve static files; route all other requests to index.php in same folder

I'm looking to make an .htaccess file that serves all static images as normal, but routes any other requests to the index.php file that's in the same folder as said .htaccess file. This is my code so far, which doesn't seem to work. It defers to an index.php that's higher up in the folder structure.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
RewriteRule ^index.php/?(.*)$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
As this is at sub-folder level, the issue is that the RewriteBase is pointing at the root directory. You can either remove the RewriteBase directive, or change it to be the folder that the .htaccess file is in so the rewriting can work from that point.

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

RewriteRule relative to subdirectory the htaccess is in

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]