Htaccess - remove .php extension and keep query - apache

I've tried to remove the .php extension and keep the id query parameter, but I get a 404 error.
This is the URL:
example.com/folder/file.php?q=123
And I want it to be:
example.com/folder/file/123
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^$1/(.*)$ /$1.php?q=$2

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This would internally rewrite the URL from /folder/file/123 to /folder/file/123.php, which is obviously going to result in a 404. It would be preferable to test that the corresponding .php file exists, rather than testing that the request does not already map to a file.
I'm assuming the q URL parameter always takes a numeric value.
Try the following instead:
# Rewrite "/folder/<file>/<123>" to "/folder/<file>.php?q=<123>"
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/(\d+)$ $1.php?q=$2 [L]
The NC flag is not required.
RewriteRule ^$1/(.*)$ /$1.php?q=$2
This rule doesn't make sense and is redundant.

Assuming last path component makes query parameter q, you can use this rule:
RewriteEngine On
# ignore all files and directories from rewrite
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Add .php extension and use last component as parameter q
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/([\w-]+)/?$ $1.php?q=$2 [L,QSA]
# Add .php extension
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

mod_rewrite omit html extension loop

I have the following scenario:
Two frontcontrollers in a web directory (document root):
web/frontend.php # handles all *.html requests
web/backend.php # direct calls only
Rewriting is easy so far:
RewriteCond %{REQUEST_URI} !^/backend.php
RewriteRule (.+)\.html$ /frontend.php [L]
So now when I call example.org/backend.php I'm in the backend, nothing special happens. And when I call something like example.org/ or example.org/team/john.html it is handled by frontend.php.
Works so far!
Now I want the possibility to omit the *.html extension so that example.org/team/john is internally handled as example.org/team/john.html.
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_URI}.html [L]
Last but not least I want to redirect requests to john.html to john to avoid duplicate content.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.+)\.html$
RewriteRule (.*)\.html$ /$1 [R=301,L]
Every part works on it's own but put together I get a loop, which doesn't surprise me but I don't know how to avoid this. I searched the docs, tried several flags and conditions but I'm totally stuck and I need help.
Here is the whole .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# extend html extension internally
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_URI}.html [L]
# redirect example.html to example
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.+)\.html$
RewriteRule (.*)\.html$ /$1 [R=301,L]
# frontcontroller
RewriteCond %{REQUEST_URI} !^/backend.php
RewriteRule (.+)\.html$ /frontend.php [L]
</IfModule>
Any help would be great.
To avoid a loop, you can use THE_REQUEST
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} "\.html "
RewriteRule ^(.*)\.html$ /$1 [R,L]
Unrelated, but you can simplify your rules. First one
RewriteCond %{REQUEST_URI} !^/backend.php
RewriteRule (.+)\.html$ /frontend.php [L]
You already check for (.+)\.html, so you can omit the RewriteCond. Next, you don't use the captured part (.+). Replace it with . to ensure it's not empty. This gives then
RewriteRule .\.html$ /frontend.php [L]
Second one, unless you have *.html.html files in your site, you don't need to check for !html and can just use ^ for the RewriteRule pattern
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
The loop is because of the multiple internal redirections, You can use END flag to prevent the rewrite loop
RewriteRule ^(.+)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_URI}.html [END]

mod_rewrite rewrites to directory twice?

My index page and links like /page work fine, however my rewrite rule to remove the php from the /page.php seems to be writing the url incorrectly.
Example: http://www.example.com/folder/page.php will redirect to http://www.example.com/folder/folder/page which does not exist
# Remove .php extention
RewriteCond %{THE_REQUEST} \s/(.+?)\.php(?:\s|\?) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1? [R=301]
# Rewrite to PHP file extension (if existing) without changing url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
Your problem is that you put the above code in .htaccess file in this directory http://www.example.com/folder/ so when a page comes without .php it will be handled fine for example if you request http://www.example.com/folder/page it will give you this result http://www.example.com/folder/page , without .php because it will be handled by the second section of your given code
# Rewrite to PHP file extension (if existing) without changing url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
it means when file comes without .php will result the same file , good.
but the problem when you write the same request along with .php so the request will be http://www.example.com/folder/page.php and this according to section one of your code
# Remove .php extention
RewriteCond %{THE_REQUEST} \s/(.+?)\.php(?:\s|\?) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1? [R=301]
will immediately remove the extension .php correctly but the problem is in line of code:
RewriteRule ^ %1? [R=301]
The path in a RewriteRule target is incorrect so you see it twice and you should add
RewriteBase /
line above
RewriteRule ^ %1? [R=301]
or just replace it with :
RewriteRule ^ /%1? [R=301]
and it will work fine . `
You can use these rules:
# remove .php extension
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.php[\s?/] [NC]
RewriteRule ^ %1? [R=301,L]
# Rewrite to PHP file extension (if existing) without changing url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
Make sure to clear your browser cache before testing this.

How to rewrite URL paths to query strings

Here is my .htaccess file:
# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]
# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# To check whether the PHP file exists and set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]
# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Assume there is a file.php in the root directory and v1,v2,... are my query string inputs and the URL below is requested:
http://domain.tld/file/v1/v2...
I simply need to send v1,v2,... as HTTP GET to the file.php using .htaccess and it should work with any other PHP file names and in case of possibility any number of inputs, otherwise handling up to 3 inputs is enough.
I assume that file.php is just a template name, and is not limited to that particular name. You can try:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/(.+)$ $1.php?params=$2 [NC,L]
Now, in the file.php, you will get the passed query arguments as:
$_GET['params'] // which will be of the form v1/v2/v3...

.htaccess URL re-write - strip .php suffix

I want to rewrite URLs like this:
http://domain.com/category.php?id=xx
to:
http://domain.com/category/?id=xx
Also, I want to hide the index.php. How can I achieve this with .htaccess? The following code doesn't work:
##REDIRECTS-START
##REDIRECTS-END
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(ADMIN.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
### ROOT DIR TO PROJECT
RewriteRule . wallkill/index.php [L]
Thanks.
Try this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
reference link
You need 2 sets of rules. The first set redirects the browser to requests without the .php at the end, then internally rewrites the .php back. The second rule redirects the browser to requests without the index.php.
So something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(.*)index\.php
RewriteRule ^ /%1 [L,R]
RewriteCond %{THE_REQUEST} \ /+([^\?]+)\.php
RewriteRule ^ /%1/ [L,R]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Note that when you add that / trailing slash to the end, your relative URL base changes, and all your relative links will break. To fix this, either make sure all your links are absolute (start with / or http://) or add a relative base to the header of all your pages:
<base href="/" />

mod_rewrite redirect url if no file found to main page

I want to be able to Access my scripts without the .php, .html etc., so I already wrote
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
##same for other extensions
in my .htaccess file (note: this file lies not in the root-path), but I also want to Redirect every incorrect request to my main page, so that www.mysite.com/dir/incorrect will be rewritten to www.mysite.com/dir/.
But my first try (RewriteRule ^ / [R] after RewriteCond) redirected me to www.mysite.com/, my experiments with RewriteBase (RewriteBase . and RewriteBase /) didnt work and I also noticed that many similar scriptredirect to www.mysite.com/dir/index.php (www.mysite.com/dir/index in my case), but I really want to Redirect to www.mysite.com/dir/. Is there any way to achieve this?
Have it this way:
RewriteEngine on
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]