.htaccess Internal Server Error when i use 2 link structure - php

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]

Related

How to write a simple .htaccess with two entry points

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]

Error getting value of a query string

I removed all files with .php extension on my project folder using this .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
Now, for example, home/product.php becomes home/product.
However I have a problem, I have a url with a query string like this - "home/product_details.php?id=$id". I can only access "home/product_details". I want it to be like "home/product_details/$id".
Please how do I do it and how can I get the value of $id?
I would usually do:
$id = $_GET['id'];
I think I need to adjust something in the .htaccess but I don't know.
Thank you.
You can use the following rules :
RewriteEngine on
#remove .php exten
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*?)/?$ $1.php [L]
#rewrite /file/foobar to /file.php?id=foobar
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ /$1.php?id=$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]

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]

htaccess behavior when accessing root

I've been using this htaccess code to pass on vars to redirect.php which handles the includes for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)/(.*)$ redirect.php?value1=$1&value2=$2&value3=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ redirect.php?value1=$1&value2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ redirect.php?value1=$1 [L,QSA]
but i noticed that it will not go to redirect if all arguments is empty, so if i go to http://domain.com/ it will open index.php, but if i go to http://domain.com/any-param/ redirect.php handles it correctlty. How can I make it always use redirect.php as default, even when no additional URL parameters is set?
Your rules appear to be fine to me. Just add this line in the end:
RewriteRule ^$ redirect.php [L]
This will redirect http://domain.com/ to http://domain.com/redirect.php.
use to do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z]+)?/?([a-z]+)?/?([-a-z0-9]+)?/?$ [NC]
RewriteRule ^([a-z]+)/?([a-z]+)?/?([-a-z0-9]+)?/?$ /redirect.php?value1=$1&value2=$2&value3=$3 [NC,L]
Note: Even with only 1 param, this will still work as it just leaves the other 2 blank. So you don't have to copy and paste thew same code while removing just one condition.
I found that this is not very scalable. Now I just redirect everything to the index.php and let that file fetch and dissect the URI to handle my system layout. This allows for a very scalable system.

Categories