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

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).

Related

.HTACCESS Redirection Issues

Namely, I have this .htaccess command...
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^\x00-\x7F]+).*$ ?open=encyclopedia&letter=$1&term=$0 [R,B,L,QSA]
RewriteRule ^([A-Z](?:[^\x00-\x7F]+|[A-Z])?).*$ ?open=encyclopedia&letter=$1&term=$0 [R,B,L,QSA]
...And I have two issues with it now:
Issue 1 - It loads shorthand of all of the letters except A, S and O. It displays blank white page instead of the actual page.
Issue 2 - When I enter http://example.com/Šandi (Astronomy), instead of redirecting me normally to http://example.com/?open=encyclopedia&letter=Š&term=Šandi+(Astronomy), the URL is http://example.com/?open=encyclopedia&letter=%25c5%25a0&term=%25c5%25a0andi+%2528Astronomy%2529
In other words:
• When I remove the [R] flag from either of the Rules, the shorthand URL (id est - example.com/A-O-S) is as described in Issue 1.
• When I add the [R] flag, it redirects inappropriately, as described in Issue 2, and it does not display the page.
Note: These problems include English and non-English letters (Š/Đ/Č/Ć/Ž).
As #anubhava suggested:
<?php print_r($_GET); ?> with [R] flag:
English: Array ( [open] => encyclopedia [letter] => V [term] => Vodolija (Astrologija) )
URL: ?open=encyclopedia&letterV&term=Vodolija (Astrologija)
Non-English: Array ( [open] => encyclopedia [letter] => ? [term] => Škorpija (Astrologija) )
URL: ?open=encyclopedia&letter=%c5&term=%c5%a0korpija%20(Astrologija)
<?php print_r($_GET); ?> without [R] flag:
English: Array ( [open] => encyclopedia [letter] => V [term] => Vodolija (Astrologija) )
URL: /Vodolija (Astrologija)
Non-English: Array ( [open] => encyclopedia [letter] => Š [term] => Škorpija (Astrologija) )
URL: /Škorpija (Astrologija)
As for the Index pages:
A - Array ( [open] => encyclopedia [letter] => A [term] => A ): It doesn't display the page, it shows blank white page.
O - Array ( [open] => encyclopedia [letter] => O [term] => O ): It doesn't display the page, it shows blank white page.
S - Array ( [open] => encyclopedia [letter] => S [term] => S ): It doesn't display the page, it shows blank white page.
As for the all the other Index pages, URL is Array ( [open] => encyclopedia [letter] => AnyLetter [term] => SameAnyLetter): It displays the page correctly... ...I do not see logic in it.
This is the new code:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#1 RewriteRule ([A-Z](?:[^\x00-\xFF].|\w).*) ?open=encyclopedia&letter=$2 [R,B,NE,L,QSA] # it loads DZH, but not the rest
#2 RewriteRule (([\x00-\xFF].|\w).*) ?open=encyclopedia&letter=$2 [R,B,NE,L,QSA] # it loads A-O-S and it loads DJ-ZH-LJ-NJ-SH
RewriteRule ^([A-Z](?:[^\x00-\x7F]+|[A-Z])?).*$ ?open=encyclopedia&letter=$1&term=$0 [B,NE,L,QSA]
When I delete the line #1, the line #2 is working fine. When I delete the line #2, the line #1 is working okay. But they won't work together.
The line #2 displays all the letters except for the DŽ, and redirects properly.
The line #1 displays only the letter DŽ properly, won't redirect and won't load the rest.
With the line #2 enabled, the URL with /DŽ redirects to /?open=encyclopedia&letter=D%c5
UPDATE #6:
With the following code...
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ((LJ).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA]
RewriteRule ((NJ).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA]
RewriteRule ((D\xC5\xBD).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA]
RewriteRule (([\x00-\xFF].|\w).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA]
...This happens:
• INDEX Pages: As for the „double-letter“ letters, it opens every letter correctly. As of the single letters, it loads every letter except A, O and S.
• TERM Pages: Considering it won't open A, O and S, therefore it won't open their Terms pages.
• /Lav (Astrologija) doesn't redirect to /?open=encyclopedia&letter=L&term=Lav (Astrologija), but instead redirects to /?open=encyclopedia&letter=L&term=Lav+%2528Astrologija%2529 thus won't open the proper page.
• It opens /Š properly and it redirects /Škorpija (Astrologija) properly to /?open=encyclopedia&letter=Š&term=Škorpija (Astrologija)
• It opens /Đ properly and it redirects /Đavo properly to /?open=encyclopedia&letter=Đ&term=Đavo
• Although it opens /U properly, /Unuk Al Haj redirects to /?open=encyclopedia&letter=Un&term=Unuk%20Al%20Haj, not opening the right page.
• Although it opens /U properly, /Ugaoni Razmak redirects to /?open=encyclopedia&letter=Ug&term=Unuk%20Al%20Haj, not opening the right page.
• Although it opens /R properly, /Ribe (Astrologija) redirects to /?open=encyclopedia&letter=Ri&term=Ribe (Astrologija), not opening the right page.
• Although it opens /V properly, /Vaga (Astrologija) redirects to /?open=encyclopedia&letter=Va&term=Vaga (Astrologija), not opening the right page.
• Although it opens /U properly, /Umerenost redirects to /?open=encyclopedia&letter=Um&term=Umerenost, not opening the right page.
• Although it opens /K properly, /Kuće (Astrologija) redirects to /?open=encyclopedia&letter=Ku&term=Kuće (Astrologija), not opening the right page.
Use NE (no escape) flag:
# here are all 2 letters which we count as 1
RewriteRule ((D\xC5\xBD).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA] # C5 BD is utf8 encoding of Ž
RewriteRule ((LJ).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA]
RewriteRule ((NJ).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA]
# one letter
RewriteRule (([\x80-\xFF].|\w).*) ?open=encyclopedia&letter=$2&term=$1 [R,NE,L,QSA]
It seems to me that you should be able to use:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.)[^/]*$ ?open=encyclopedia&letter=$1&term=$0 [L,QSA]

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]

.htaccess rewrite rule not reading the last two parameters

I have this rules in my .htaccess
RewriteRule ^clothes/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ products.php?g=$1&$2=$3&$4=$5&$6=$7&$8=$9&$10=$11 [L]
RewriteRule ^clothes/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ products.php?g=$1&$2=$3&$4=$5&$6=$7&$8=$9 [L]
RewriteRule ^clothes/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ products.php?g=$1&$2=$3&$4=$5&$6=$7 [L]
RewriteRule ^clothes/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ products.php?g=$1&$2=$3&$4=$5 [L]
RewriteRule ^clothes/([^/]+)/([^/]+)/([^/]+)/?$ products.php?g=$1&$2=$3 [L]
RewriteRule ^clothes/([^/]+)/?$ products.php?g=$1 [L]
which can be something like website.com/clothes/women/type/tshirts/brand/nike/color/red/size/s/price/0-29
and this should be the print_r($_GET)
Array
(
[g] => women
[type] => tshirts
[color] => red
[brand] => nike
[size] => s
[prezzo] => 0-29
)
So starting from website.com/clothes/women/ I can then add filter/value pairs and arrive at the point where I can have that big url with all the possible filters.
The problem is that the last filter/value pair is not working, this snippet from the first rule in .htaccess [..]&$10=$11. So instead of the aspected dumped data, I get this
Array
(
[g] => women
[type] => tshirts
[color] => red
[brand] => nike
[size] => s
[women0] => women1
)
So basically it replaces the last filter/value pair with the name of the first filter, g=$1.
Is there a limit withing .htaccess of how many parameters I can pass into the url ? It seems to me that the last parameter is $9, so $10 is not taken into consideration.
If that is the case, can I fix this problem ?
Thank you.
You can overcome this mod_rewrite by having this recursive rule. Place this in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(clothes/[^/]+)/([^/]+)/([^/]*)(/.*)?$ $1/$4?$2=$3 [L,QSA,NC]
RewriteRule ^clothes/([^/]+)/?$ products.php?g=$1 [L,QSA,NC]

Rewrite rule with redirect in php

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");

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