Htaccess - redirect 'files' with no file extension - .htaccess

I want to redirect only php files and files with no file extension like these:
http://www.test.com/test.php
http://www.test.com/TEST
I have this htaccess script, which redirects php files to target.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^\.]+\.php$ /target.php [L,NC]
Now I need to add files with no extension to this redirect.
Thanks for help!

These rules should do the job.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^\.]+\.php$ /target.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]*[^/\.]$ /target.php [L,NC]
The redirect will work for http://www.test.com/TEST as long as you do not have folder or file named TEST in your site.

Related

Redirect to file if file doesnt exist and keep url

i have an index.php in the following folder:
https://example.com/folder1/folder2/index.php
If the URL is
https://example.com/folder1/folder2/93j3h233j3
then redirect to the index.php but the URL should stay the same!
My idea: i call a php file without .php first and then try to redirect...
RewriteRule ^folder1/folder2/([^\.]+)$ /folder1/folder2/index.php?&%{QUERY_STRING}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder1/folder2/(.*)$ /folder1/folder2/index.php? [R=302]
This doesnt work.
And keep the name! but how?
Any Idea?
Thank you
You can have your rule as this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder1/folder2)/.+$ $1/index.php [L,NC]
There is no need to use your first rule.

How can change url link with htaccess

I have PHP file page.php, and with this file I dynamically change content. So how can I make with htaccess to change link from www.example.com/page.php?page=somepage to www.example.com/somepage?
Add the following code to your htaccess file :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /page.php?pape=$1 [NC,L]
This will rewrite your url
example.come/somepage
to
example.come/page.php?page=somepage

Use .htaccess file to check folder for file and redirect if not found

I want to use .htaccess files so that if someone visits an old URL on our new website such as website.com/oldpage.asp .htaccess first checks to see if website.com/oldsite/oldpage.asp is on our server. And if no file is there redirect them to old.website.com/oldpage.asp where a copy of our old website exists.
For some reason only the .htaccess file in the /oldsite/ folder is executing.
Here is my .htaccess file for my site root which checks if the file exists on the new site and if not redirects to /oldsite/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /oldsite/$1 [NC,QSA,L]
Then in /oldsite/ its supposed to check if the file again exists, and if not redirect again.
RewriteEngine On
RewriteOptions inherit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,NC,QSA,L]
You don't need 2 .htaccess for this.
Have this rule in site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/oldsite/$1 -f
RewriteRule ^(.*)$ oldsite/$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,L]
Then remove /oldsite/.htaccess and test this after clearing your browser cache.

.htaccess redirect non-existing pages only, keeping the token

I want to redirect
www.domain.com/abc
to
www.domain.com/index.php?a=abc
only if the folder abc doesn't already exist.
How should I write the .htaccess file?
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ http://www.domain.com/index.php?a=$1 [L]

mod_rewrite won't remove file extensions with other rewrite rule in place

I have the following mod_rewrite within my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]+)$ index.php?ID=$1 [QSA,NC,L,R]
The code above works but the issue is several of the pages on my site link to files without extensions because our original .htaccess file removed all extensions from every request. My site is built on PHP. Ultimately I want to use the rules above exactly as is but at the end of it all, i want to strip the file extension (.php) from every request.
Is this possible?
Try this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^index.php
RewriteRule ^(.*)\.php$ $1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]+)$ index.php?ID=$1 [QSA,NC,L,R]

Resources