how to remove .php extension from the url - apache

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

Related

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

Conditional URL rewrite

I need the URL example.com/app to show the content from example.com/app/dist
The following code does exactly this.
RewriteEngine On
RewriteRule ^$ /app/dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/dist/
RewriteRule ^(.*)$ /app/dist/$1
BUT I also need the URL example.com/staging to show the content from example.com/staging/dist
With the current setup, the url example.com/staging is showing the content from example.com/app/dist
The .htaccess while is located in the /app and /staging folder (but it has to be the same file)
In the htaccess file in your staging folder, change all instances of app to staging
Edit:
You can try using relative paths instead of hardcoding the app or staging, but Ive had issues in the past, especially if there are other rules in play.
RewriteEngine On
RewriteRule ^dist - [NC,L]
RewriteRule ^$ dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dist/$1 [L]
The idea is to remove the leading / from rewrites so a relative path is used. So technically it wont matter whatever folder the htaccess file is in

.htaccess affect other file url

I am trying to make .htaccess rule not affect other file url
example
my .htaccess rule is
RewriteEngine On
RewriteRule ^([^/]*)$ /tr/hp.php?q=$1 [L]
my site url is
mydomain.com/keywords
everything working good on keywords but when I try to open robots.txt
mydomain.com/robots.txt
OR
mydomain.com/images.jpg
any other file url
redirect on /tr/hp.php?q=filename
which .htaccess Rewrite Rule works on both?
Try :
RewriteEngine On
#--exclude real directories
RewriteCond %{REQUEST_FILENAME} !-d
#--and files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /tr/hp.php?q=$1 [L]
You also have to prevent matching any request pattern that carries a dot in it:
RewriteEngine On
RewriteRule ^([^/.]*)$ /tr/hp.php?q=$1 [L]
Certainly it is possible to further refine this pattern.
Alternatively some people like to prevent rewriting requests to files or folders that physically exist in the file system using RewriteCond:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
But that is something you have to decide upon. This does not help for example if resources like robots.txt are delivered in a dynamic manner...

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]