htaccess change .html to .php from all files inside folder - php

so i have this folder inside my site.. and i had to change all file types from html to php but inside this static html i have links to this files using html as extension... the thing is that i have php now.
is it possible to write htaccess rule for this folder so any link to .html will become .php?
www.site.com/folder1/folder2/something.php
inside something.php i have a link to: www.site.com/folder1/folder2/somethingelse.html (but its actually .php now) and i have also hundred of files that have been change to php
i have tried lots of rules but im really noob with them, last one i have tried was
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website.net
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) http://www.website.net/folder1/folder2/$1 [R=301,L]
RewriteRule ^(.+)\.html$ http://www.website.net/folder1/folder2/$1.php [R=301,L]
but it doesn't work!
please help!
UPDATE: MICHAELOKGB answer was almost ok! the only thing left to do was to set as rewritebase the actual folder: "/folder1/folder2/" and its working like a charm! thanks guys!!

Use this. I tested it and it works.
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} folder1/folder2/(.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} folder1/folder2/(.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
Edit: Forgot to add specific folder only. If you remove /folder1/folder2/ it will apply to whole site.
Edit2: So by response, it looks like you should change rewrite base from / (whole site) to praticular folder. Its weird, as I tested it for specific folder and it worked, but anyway:
RewriteBase /folder1/folder2

Related

Redirecting existing URLs to SEO Friendly URLs

I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1

Possible to link to CSS file despite htaccess redirect?

This question has been asked before, but the answers posted don't seem to work for me. I hoping for some troubleshooting.
I have an .htaccess file to redirect ALL requests to my index.php file, with the request path stored as a variable. I want, however, to exclude a single directory (/admin/assets) from this process so that I can easily link to css files, etc.
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_URI}!^(assets)
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
The above continues to redirect all traffic to the index.php file, but the linked-to CSS file (/admin/assets/main.css) is not loaded. What, if any, way is there around this?
Your %{REQUEST_URI} contains /admin/, even if you set the RewriteBase.
Plus, you missed a space in your RewriteCond.
You need tu use this .htaccess :
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_URI} !^/admin/assets
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

url rewrite and redirect with folder using htaccess

OK I'm new with the issue of URL rewriting and redirecting. I've search up and down on Google & Stackoverflow for an answer that would work and nothing seems to work!!
What i'm trying to accomplish is...
Take this link: mysite.com/research/index.php?quote=goog
Then convert it and redirect it to this: mysite.com/research/goog
Does it matter that the "goog" at the end of the URL string is grabbed from a form placed in the url? here is the code used to grab the "goog" <?php echo $_POST['quote'];?>
Below is the only snippet code that I have on my htaccess file and it won't work! Am I doing it wrong? Is there something missing from my code? I have my code place on the root directory (mysite.com) should i have it in the "research" folder? (mysite.com/research/)
RewriteEngine On
RewriteRule ^quote/([^/]*)$ /research/index.php?quote=$1 [L]
Is it possible my host / server doesn't accept .htaccess files? should I do it in a web.config file? If so how would I convert the above code to a working web.config file?
These are the rules you will need in your /research/.htaccess file:
RewriteEngine On
RewriteBase /research/
RewriteCond %{THE_REQUEST} /index\.php\?quote=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?quote=$1 [L,QSA,NC]
Access it in your index.php using:
$quote = $_GET['quote'];`
This should work for you:
RewriteEngine On
RewriteCond %{QUERY_STRING} "quote=([^&]+)"
RewriteRule ^/research/index.php /research/%1? [R,L]
The query string is a separate part of the request to the URL so you need to check that explicitly. By using the RewriteCond, you can say if the 'quote' parameter exists, you'll capture it's value (by getting all characters that follow it that are not '&'). This populates the %1 variable which you can use in the rewrite itself.

.htaccess being ignored? Not redirecting like it's supposed to

Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.

Linking to file without extension (.php removed through .htaccess) not working on one page

I succeeded in making .htaccess rewrite URLs so I can link to pages without having to define the extension. But, only one of the pages won't work.
The page is called 'games.php' and I link to it through Go to Games!
But whenever I go to it, it goes to '/games/', as if it were a folder, and then gives an Error 404.
I don't know how to fix it. I checked, there's no folder or any other file called like this, and it works on all the other pages... (also on games?id=blabla...)
Here's my .htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
Thanks in advance,
Dalionzo :D
RewriteEngine On
RewriteBase /
RewriteRule ^games(.*)?$ games.php$1 [L,QSA]
RewriteRule ^games(/)?$ games.php [L,QSA]
UPDATED
Now it works without parameters too.
website.com/game.php?id=blablabla --> website.com/game?id=blablabla

Categories