.htaccess to customize php url in folder - apache

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]

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]

I want to rewrite https://blog/home/post?read=example to https://blog/home/example with .htaccess

I have tried many methods but none has worked.
Blog is the name of the domain, Home is a folder in the domain and post.php is the page getting details from database.
So, in my domain, I have:
home/post.php
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)$ /post?read=$1 [L] # Handle page requests
Above is the last code I used and it is not working. I'm getting a 404 error.
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)$ /post?read=$1 [L] # Handle page requests
The .htaccess is located in the root of the folder.
You seem to be ignoring the home subdirectory. (Although you've referred to both home and Home in your question - note that this is case-sensistive.) And if "/post is a PHP file" then you should presumably be rewriting to post.php, not simply post?
Note that Apache does not support line-end comments, as you are using here. (At best they get ignored, at worst they trigger a 500 error.)
If the .htaccess file is located in the document root, as you suggest then you would need to write the rule as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/([\w-]+)$ home/post.php?read=$1 [L]
The \w shorthand character class is the same as [A-Za-z0-9_].
The RewriteRule pattern matches the URL-path relative to the location of the .htaccess file.
If, on the other hand, the .htaccess file is in the /home subdirectory (ie. at /home/.htaccess) then you would write the rule like this instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ post.php?read=$1 [L]
Note, there is no slash prefix on the substitution string.

Rewrite rule to redirect two friendly urls to same file

I'm trying to redirect two different url to the same file.
The url are: http://localhost/CLIENTI/HOTEL/cp/amministratori and http://localhost/CLIENTI/HOTEL/cp/amministratori/test
Actually i currently see the first url, but when I open the second one i see a lot of error of files inclusion on client side (es js and css files on header/footer).
This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dashboard$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^amministratori$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^amministratori/test$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
that was all my htaccess code. The errors are 404 of bootstrap.js and main.js which contains custom code for the frontend. I've understood the problem but I've haven't undersood if there is a solution alternative to the leading slash, because my header and footer are included in php, so in /cp/amministratori they work as now, but obviously if I change with a leading slash they will not work anymore
With your shown attempts, please try following htaccess Rules. Make sure to place your htaccess file along with CLIENTI folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /CLIENTI/HOTEL/cp/amministratori/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dashboard/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [QSA,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

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

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