How to write a simple .htaccess with two entry points - php

I have problem while trying to use below .htaccess file to direct my requests. every one begin with rest to rest.php and others to index.php. the problem is all request directed to index.php even the ones begin with rest. did i miss something ?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^rest(.+)$ rest.php? [QSA,L]
RewriteRule ^(.+)$ index.php? [QSA]

below .htaccess seems to solve my problem, please if there is a bug, write it in comment.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^rest$ rest.php? [QSA,L]
RewriteRule ^rest(.+)$ rest.php? [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php? [QSA]

Related

How to change the URL in htaccess

I have created all the file in my folder
Now when I am accessing the product_2.php I am getting the URL
http://localhost/Test/product_2
Now I have to display the URL like
http://localhost/Test/product/product_2
I added RewriteRule ^product/product_2 product_2.php in the .htaccess but it's not working.
.htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^product/product_2 product_2.php
Would you help me out in this issue?
Try adding the following line to your .htaccess file.
Redirect /Test/product/product_2 http://localhost/Test/product_2
You should put it before and with a little change
RewriteRule ^product/product_2 product_2 [QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Anyway, this is not the best practice I would recommend.. you should redirect all of your calls to index.php and handle them there, something like that:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
then in index.php
$requested_url = $_GET['url'];
and from here you can decide what to do, you can include the wanted file or even do something more clever.

.htaccess Internal Server Error when i use 2 link structure

Well,
This is my actual url:
http://www.domain.com/wix/t1/index.php?username=foyezbd (this foyezbd is come from variable $username)
and I re-write it with .htaccess
http://www.domain.com/wix/t1/foyezbd
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wix/t1/index.php?username=$1 [L]
Now I've another page which is page.php
so the actual link for this page might be..
http://www.domain.com/wix/t1/page.php?username=$username&menu_name=$menu_name
so I want to re-write this url with following structure: (For example $username = foyezbd and $menu_name = About us)
http://www.domain.com/wix/t1/foyezbd/aboutus
How can i do this with .htaccess ? I added a new line like:
RewriteRule ^(.*)$ /wix/t1/page.php?username=$1 [L]
but it says Internal Server Error!
You're getting a server error because the rule is looping. You don't have conditions to prevent this like you do for the first rule. Additionally, (.*) is too general if you want to be able to differentiate between the two:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /wix/t1/page.php?username=$1&menu_name=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /wix/t1/index.php?username=$1 [L]

apache htaccess codeigniter query string to path

i'm trying to make a codeigniter based website be able to convert index.php?option=test&controller=con1 to /test/con1 using .htaccess i tried multiple examples i found and none seem to work, it goes straight to the home page. any ideas?
i've tried things like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^\/(.+?)\/(.+?)\/? /index.php/$1/$2 [L]
but doesn't throw any errors or anything.
my current htacces is
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|img|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
try put this .htaccess in same directory with your index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Change URL name with mod rewrite on htaccess

I am having a tough time to understand how mod_rewrite works.
I have a url like this:
www.example.com/store/index.php?categ=headbands
And I want to make it look like this:
www.example.com/store/headbands
I am trying this, but not working:
RewriteEngine on
RewriteBase /store/
RewriteRule ^index/([^/\.]+)/?$ $1 [L]
And I confirmed the mod_rewrite is activated with a random example I found.
Any suggestions?
Thanks!
Try This one
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/store/(.*)$ /store/index.php?categ=$1 [NC,L]
OR
Put the .htaccess file inside the "store" directory with following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?categ=$1 [NC,L]

multiple dynamic url rewriting redirect to 404 page

I am doing a project with fetching data from database. I am used URL rewriting method. I was using rewrite method it will redirect to error page
Dynamic url
http://www.sample.com/?cat=kk
The rewritten URL
http:/www.sample.com/kk
the .htaccess file written for this URL rewrite
RewriteEngine On
RewriteRule ^([^/]*)$ /?cat=$1 [L]
The .htaccess file also contain another url rewrite
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&year=$3&page=$4 [L]
RewriteRule ^([^/]*)/([^/]*)$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&pag=$3 [L]
RewriteRule ^([^/]*)/$ /?cat=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The problem I am having is that when I access any page, it will redirect to server default error page.
I'm sorry to say that, but your rewriterules are messy.
Always start from the most complex to the simplest one.
And you've forgotten one of the most important directives: QSA. I let you google for this ;)
Maybe what follows still doesn't work, but it's cleaner.
We need log files to know what's happening.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&year=$3&page=$4 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&page=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /?cat=$1&sub=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /?cat=$1 [QSA,L]

Categories