how to remove index.php?file= using .htaccess to get last url - php

Here, I want to remove index.php?file= for every urls of my website. I have tried many tutorial of .htaccess,but unlucky to create correct one.
Actual url as below:
http://localhost/test/json/iscore2/index.php?file=home
http://localhost/test/json/iscore2/index.php?file=contact
Then I want to make like this :
http://localhost/test/json/iscore2/home
http://localhost/test/json/iscore2/contact

Probably you already have setted up rewrite module in apache2
(sudo a2enmod rewrite and host settings for AllowOverride)
I'd start with something like this in iscore2 folder
<IfModule mod_rewrite.c>
RewriteEngine on
# Play with this if redirection fails
#RewriteBase /
# if it is not a file or folder, rewrite to index.php?file=<value>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?file=$1 [L,QSA]
</IfModule>

Related

.htaccess stop directory listing and forward to handler.php

I am trying to implement SEO friendly URLs using .htaccess and PHP.
I have achieved 90% of my goal and struggling with just one use case.
Here is what happing, when I access the following url
http://somesite.com/cms/movies/tarzan
it lands on
my-handler.php?the_url=movies/tarzan
This is perfect and that is what I want because then I manage it myself. The real problem is when I don't provide any slugs, then it lists the directory (means show all files and folders in it)
http://somesite.com/cms/
Can someone please help me fix following .htaccess content, so that even if I don't provide slug it should still be handled by my-handler.php instead of lisiting full directory?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cms/my-handler.php?the_url=$1 [L,QSA]
</IfModule>
Solved
As per #Roman suggestion, I added the following to the above listing and it solved my problem. Plus I do not have to compromise on accessing physical directories RewriteCond %{REQUEST_FILENAME} !-d
DirectoryIndex my-handler.php
At first you have to set the DirectoryIndex to handle every request which points to / with your my-handler.php. This can be done by adding the line to your .htaccess
DirectoryIndex my-handler.php
To disable the directory listing, you have to forbid the listing by adding the following line to your .htaccess
Options -Indexes
Remember that this configuration is per .htaccess and just for the directory you are placing the file in. If you want to make changes for your whole webserver, you can edit the httpd.conf and search for
Options Indexes
and remove the Indexes option.
Documentation
The listing is provided by the mod_autoindex module.
Nice side-fact
If you just want to disable the listing of specific file-types like .env or .php files, you can add the option IndexIgnore *.php to you .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css|json|woff|ttf)
RewriteRule ^(.*)$ /cms/my-handler.php?the_url=$1 [L,QSA]
</IfModule>

Redirect all requests to single page

I have a food blog website, with many posts stored in a database. When someone clicks on a link to a post, the website searches the database for the post and presents it in a template. I don't store an HTML file for each post.
Right now, I do it by handling a 404 error (which goes off because the requested HTML file doesn't exist) and searching the database from there. I know this isn't right.
How can I set up my web server (Apache running on a Raspberry Pi), so that all requests go to one page, which will do the searching and send the user to the right page? And is this the right thing to do?
You can do that with a file you put in your website root named .htaccess and using the mod_rewrite module. You'll need to make sure .htaccess and mod_rewrite are enabled in the Apache configuration. But once enabled you can do something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
That will rewrite any URL that doesn't refer to an actual existing file or directory to your index.php which you can use to handle the request.
For example, if someone visits http://yourdomain.com/article, the path will be internally rewritten to index.php?path=article and you can access the path using $_GET['path'].
Note: If you want to literally serve everything through that single entry point you could remove the two middle lines and that would send everything through there. Including images, scripts, CSS etc, which is generally not what you want.
You need proper .htaccess rules in your public root directory and you need a apache2 mod_rewrite module enabled.
On Ubuntu you can can enable it with the following command:
sudo a2enmod rewrite
your .htaccess file could look like the following:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

How to add url parsing in Htaccess

I looking to parse only one specific url in htaccess in following format
https://example.com/apps/store/customdesign/shopid/2/designid/Design_10
into
https://example.com/apps/store/customdesign.php?shopid=2&designid=Design_10
I tried following things
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/customdesign$
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /customdesign.php?$1=$2&$3=$4
</IfModule>
I assume you are looking for a Mod_Rewrite solution, so first make sure you have the mod rewrite extension active, then write your URL Rewrites;
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
^apps/store/customdesign/shopid/([0-9]+)/designid/([0-9A-Z_]+)$ /apps/store/customdesign.php?shopid=$1&designid=$2 [QSA,NC,L]
</IfModule>
I have provided the solution I would most-likely use if this were my problem, however you should look into RegEx as well as your specific installation and hosting permissions before continuing with URL Rewrites. Spend some time and do some research!

Dynamic url in php without filename

I couldn't find exact answer to my question so I am posting this. Maybe it involves htaccess configuring.
What I would like is to have http://www.example.com/static_folder/dynamic_parameter
the above url should run the index.php in static_folder and I should be able to get that dynamic_parameter and output accordingly.
How to achieve this?
Thanks.
A littlebit of htaccess can do it pretty easily. As for example, save this rule in the root directory's htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/static_folder/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /static_folder/index.php [L]
</IfModule>
Then use $_SERVER['REQUEST_URI'] inside /static_folder/index.php to get the dynamic_parameter.
Is it what you are trying to achieve?
Try this in your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
then you can do:
sudo a2enmod rewrite
sudo service apache2 restart
Also check answers for this question: How to remove all .php extension from a dir and all of its subdirectories?
I believe the OP asked for index.php inside static_folder to get the variable, so, something like this, should do the trick:
RewriteRule ^static_folder/([^/]+)$ static_folder/index.php?variable=1$ [NC,L]

How to add an exception to a mod_rewrite rule (wordpress site)

I have a WordPress website im trying to call a php script that is located on the website the URL is something like this
http://example.com/folder/process.php
The problem is when I try to do this i always get redirected to the themes "not found" page.
This is the htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
If i put a test.html file in that directory it will work, and show the contents of the .html file. I think it has something to do with the .php extension it doesn't like?
If i call example.com/wp-config.php it finds that file and deosnt show a 404 page..
This Rule says "If request starts with index.php, do not rewrite and stop".
RewriteRule ^index\.php$ - [L]
Immediately before or after that line you could add something like
RewriteRule ^process\.php$ - [L]
To not rewrite requests to that particular file.
Note that you will have to put your file next to Wordpress' index.php for this to work properly.
Change your .htaccess to this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/+folder/process\.php$ [NC]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Try turning off mod_rewrite altogether in your /folder/ directory by creating an htaccess file in there and simply adding this single line:
RewriteEngine On
It will make it so any rules from the parent directory won't be applied. If the weirdness with PHP continues, you may want to check if there aren't any rules in your vhost/server config or that things like Multiviews are turned off (via Options -Multiviews).
Ok so if I took a default file that came with the WordPress install made a copy of it in the cpanel rename it remove the code and than put my code in it.
The file is found and it works. So im thinking the only thing left it could have been was a permissions problem. The permissions i put for the file when i uploaded it was 777. I know NOTHING about file permissions.
And i have been working on this for 5 hours and im done trying to figure out what the problem was it works..and im good with that.

Categories