Rewrite rule with redirect in php - .htaccess

I seem to have problem with an .htaccess rewriterule.
What I want to do is a dispatcher of this form:
[website]/[parameter1-text]/[parameter2-text]
I have in my .htaccess the following:
RewriteRule ^(.*)/(.*)$ dispatcher.php?s1=$1&s2=$2
So far so good. But s1 and s2 are some variables that need to be matched in a database (mysql) and return the true variables that feed the script view.php in the following format:
view.php?category=[s1]&subcategory=[s2]&objects=5
How can I redirect this properly using header("Location: /view.php[...]"); and having the url unchanged (for example: http://example.com/CARS/BLUE http://example.com/SLIPPERS/RED/ )

First of all fix some minor issues with your .htaccess:
RewriteRule ^([^/]+)/([^/]*)/?$ dispatcher.php?s1=$1&s2=$2 [L,QSA]
Now inside dispatcher.php do your Mysql stuff and fetch values of s1 and s2 from database. Once you're done and want to internally forward your request to: view.php?category=[s1]&subcategory=[s2]&objects=5 have this type of PHP code:
$_GET['category'] = $value-of-s1-from-DB;
$_REQUEST['category'] = $_GET['category'];
$_GET['subcategory'] = $value-of-s2-from-DB;
$_REQUEST['subcategory'] = $_GET['subcategory'];
$_GET['objects'] = '5';
$_REQUEST['objects'] = $_GET['objects'];
// now include view.php
// all your parameters are available in view.php in $_GET array
require_once("view.php");

Related

.htaccees redirect on long string

I wan't .htaccees to check it the url string is longer thnr 155 chars. If it is I want to redirect to an error page.
Her is a rule that does it (I think) for a 155 long query string, but I can't find a way to to this for a 155 and longer string. Also I am afraid that works only for url and not query string.
RewriteRule ^([a-zA-Z0-9]{155,})/?$ 404.php?u=$1 [QSA,L]
Why don't you try to preform the check in index file?
Here is an example:
$allowedL = 155;
$path = $SERVER['REQUEST_URI'];
if ( strlen($path) > $allowedL ) die();

Using OR in htaccess redirect

Following URL should be redirected:
abc-deut.html -> de/abc
abc-eng.html -> en/abc
Where deut = de
And eng = en
I am looking for something like:
RewriteRule ^abc-(deut|eng).html (de|en)/abc [R=301,L]
Does this work with htaccess?
You can use this rule:
RewriteRule ^([\w-]+)-(en|de).*\.html$ /$2/$1 [R=301,L,NE,NC]

htaccess RewriteRule URL's

I use htaccess to rewrite my URL's so here is what I have:
RewriteRule ^([^-]+),([a-z0-9._.-]+),([^-]+) index.php?go=$1&lang=$2 [L]
and code PHP:
function rewrite_seo($id, $title, $langs) {
global $core;
if($core->getSettings('seof')) {
$result = $id.','.$langs.','.changeSigns($title).'';
} else $result = '?go='.$id.'&lang='.$langs;
return $result;
}
My URLs look like:
http://localhost/script/12,en,home
but want to look like this:
http://localhost/script/en/home,12
how to modify this code?
localhost/script/12,en,home TO
localhost/script/en/home,12
Did u try to modify line 4?
Put that:
$result = $langs.'/'.changeSigns($title).','.$id;
Please take a look to PHP.net webpage http://php.net/manual/de/internals2.opcodes.concat.php
You have to change the links written in PHP as pointed out in the previous answer :
$result = $langs.'/'.changeSigns($title).','.$id;
And then change the matching rule in your htaccess :
RewriteRule ^([a-z0-9._.-]+)\/([^-]+),([^-]+) index.php?go=$2&lang=$1 [L]
Even better, you can make more specific capture regex, so it would be easier to read and understand :
RewriteRule ^([a-z]+)\/([0-9]+),([^-]+) index.php?go=$2&lang=$1 [L]

How can I go back to showing the content of /index.php?

Here's part of my .htaccess file:
.htaccess:
RewriteRule ^([a-z0-9A-Z_-]+)$ /article/index.php?title=$1 [L]
But the problem is that when title is empty it doesn't show me the content of http://www.domain.com/index.php. I can't even link to http://www.domain.com/ anymore without throwing the error.
PHP: http://www.domain.com/article/index.php
<?php
if(!empty($_GET['title']))
{
$a = mysql_query("SELECT * FROM `articles` WHERE `title` = '".mysql_real_escape_string($_GET['title'])."'");
}
else
{
$a = mysql_query("SELECT * FROM `articles` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."'");
}
$b = mysql_fetch_assoc($a) or mysql_error();
?>
You pretty much have 2 choices:
1. Use this rule: If home page is hit, then title parameter will be empty (so you have to handle this moment somehow -- adjust your PHP code accordingly).
RewriteRule ^([a-z0-9A-Z_-]*)$ /article/index.php?title=$1 [QSA,L]
2. Use specific rule for home page/website root hit (add it before your current rule):
RewriteRule ^$ /article/index.php?title=home [QSA,L]
The title=home means it's a hit for website root (you can change home to whatever you want).

How do I get next parameter in my .htaccess

Current URL
http://domain.com/page/2/
.htaccess
RewriteRule ^page/([0-9]+)/$ /index.php?p=$1 [L]
Next URL
http://domain.com/page/2/?by=1
How to get the by=1?
Because when I print_r($_GET) It's only display Array ( [p] => 2 )
What should I write in my .htacces to get the [by] => 1
Change the end of your line from [L] to [QSA,L] to preserve the query string.

Resources