Replace the filename in the URI using htaccess - .htaccess

In my website, the main PHP script is named index.php. All URIs look similar to this:
index.php?section=FrontPage&l=ES_LA&view=multiple
In the htaccess file and using the mod_rewrite extension, I managed to remove the .php PHP extension, so the URI looks like this:
index?section=FrontPage&l=ES_LA&view=multiple
What I want to do now is to replace index.php with example in the path.
I've tried using:
RewriteRule ^example$ index.php
But when I try accessing example?section=FrontPage&l=ES_LA&view=multiple the server returns a 404 Not Found status code.
How can I fix this?

Related

Rewrite engine not working using htaccess

I am trying to rewrite url using htaccess but it's saying 404.
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ product1.php?pid=$1
It's not opening product1.php file.
You need to make sure your htaccess file and your product1.php file are in same folder. If not that it will give 404 errors.
If they are not present in same folder then you need to complete relative path from the path where your htaccess file is present like: eg: Let's say we have folder structure like:
/root/.htaccess
/root/singh/test/product1.php
So have your rules in following manner then:
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ singh/test/product1.php?pid=$1 [NC,L]
Also apart from these put NC and L flags too in your rules to do case sensitive matching and making this condition's last rule here.

htaccess RewriteRule and Redirect get not what i expecting

For example visititors type or open some url, like www.domain.com/some-url
I have one php file, that provides conent for all urls. For example, file name is show_content.php
In show_content.phpi get url, using $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'], then get necessary data from mysql.
To do all above in .htaccess i use RewriteRule ^([\w=/-]+)$ show_content.php?$1 [QSA]
So far all is ok. But problem is when i want to redirect from non existent url to another url.
I use Redirect 301 /non-existent-url /another-url.
Want to see url like www.domain.com/another-url, but i see www.domain.com/another-url?non-existent-url.
Understand that it is because of ? in show_content.php?$1, but if i remove ?, then get 404 Not Found.
How to change RewriteRule, to redirect correctly?

use .htaccess to rewrite url

Here is the rewrite in .htaccess
RewriteRule photos/([a-zA-Z]{1,}).([a-zA-Z]{1,}).([0-9]{1,}) photo.php?userfirstname=$1&userlastname=$2&usernum=$3
When the url is www.xxx.com/photos/mark.stock.1 it can go to photo.php. I use smarty as the masterplate & everything works well if the url is www.xxx.com/photo.php but not like www.xxx.com/photos/mark.stock.1
I user the firebug to check the process,found out all the css file and javascript file the url looks like www.xxx.com/photos/js/xxx.js or www.xxx.com/photos/css/xxx.css
how to solve this problem?

how to write .htaccess

I have tried but have not found proper solution for PHP .htaccess
Folder and files information:
.htaccess (on root)
/profile/personal.php
/profile/exam/exam_information.php
Sending URLs from (personal.php) and (exam_information.php)
/profile/2012/A1PPOAQU7
........\-------------/ // Friendly URL
/profile/exam/2012/A1PPOAQU7
.............\-------------/ // Friendly URL
I found, htaccess but it works for single URL only. Please check what is wrong in the code.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ /profile/personal.php?pid=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ /profile/personal.php?pid=$1

htaccess to redirect url with params

I have to redirect a url to another url with all its parameters using htaccess.
My incoming url will be something like this:
www.mydomain.com/book-it.jsp?id=3&var1=values&var2=value2...
and I want to get it in PHP (book-it.php) as something like this (with all its parameters):
www.mydomain.com/book-it.php?id=3&var1=values&var2=value2...
I was using JSP and from now on moving to PHP and we need to use same URL since this url is already published with my application and I can't change that now. I need to get that url and parameters to another file.
You have 2 main approaches:
1) Using Redirect directive:
Redirect 301 /book-it.jsp http://www.mydomain.com/book-it.php
2) Using mod_rewrite module (this needs to be placed in .htaccess in website root folder. If placed in Apache config file (inside <VirtualHost>, for example) the rules need to be modified slightly):
RewriteEngine On
RewriteBase /
RewriteRule ^book-it\.jsp$ http://www.mydomain.com/book-it.php [QSA,NC,R=301,L]
Both of them will preserve query string.

Resources