How do I fix SEO urls with lighttpd? - web

I want to rewrite my URL from ?p=pagename to a SEO friendly URL like this: /pagename
How does url.rewrite works?
I have seen an example like this but haven't figured how it works yet.
url.rewrite = (
"^/(data|install|js|styles)/(.*)$" => "$0",
"^/(.*\.php)(.*)$" => "$0",
"^/.*(\?.*)" => "/index.php$1",
"" => "/index.php"
)

The query string is not part of the URL matched by rewrite rules. You can match against the query string separately:
$HTTP["querystring"] =~ "^p=([^&]+)" {
url.rewrite = (
"/%1"
)
}

Related

Can htaccess override CodeIgniter's routes.php

I'm wondering if it's possible to override routes.php rules with htaccess in Codeigniter 3.
For example, in order to point dynamic subdomains to the same controllers and pass the subdomain as a parameter, routes.php falls short for doing this, while in htaccess is really simple to do.
Another example is to mask query strings with URL segments. Routes.php doesn't allow to use query strings, but htaccess is, again, perfect for this.
So, as a general question, is it possible to use htaccess for all routing in CodeIgniter instead of using routes.php?
I think you can use htaccess for static routing in codeigniter. But for dynamic routing application base like http://localhost/myproject/user/1
you have to use routes.php.
Codeigniter routes config is used to route module/controller/method/variable patterns.
I think, domain/subdomains go out from this config, however you could use dinamic base_url, based on $_SERVER variables, and then get the string (subdomain) from the specific controller.
From my config, on CI 2.x
$config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
$config['base_url'] .= isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443' ? ( ':'.$_SERVER['SERVER_PORT'] ) : '';
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
then do something like this...
$server = $_SERVER['HTTP_HOST'];
$domain = preg_replace('#^www\.(.+\.)#i', '$1', $server);
$domain = $this->extract_domain($domain);
$subdomain = $this->extract_subdomains($server);
function extract_domain($domain)
{
if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches))
{
return $matches['domain'];
} else {
return $domain;
}
}
function extract_subdomains($domain)
{
$subdomains = $domain;
$domain = $this->extract_domain($subdomains);
$subdomains = rtrim(strstr($subdomains, $domain, true), '.');
return $subdomains;
}

Multiple Subdomain redirect to TYPO3 and RealUrl

I have TYPO3 7.6.18 installed with realURL and two languages (en,fr) on a single tree. the installation is on subdomain fr.mysite.com with bot domains added to the root page. For both languages I need a different subdomain. On first call of en.mysite.com I need to redirect to fr.mysite.com but the URL keeps at fr.mysite.com. Clicking on a link then changes the URL correctly to en.mysite.com.
I'm aware that the following htaccess is wrong but I got no idea how it should be.
htaccess
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^ http://fr.mysite.com/%{REQUEST_URI} [P]
Adding ?L=1 to the RewriteRule solves the problem on a first call and the URL ist correctly en.mysite.com but then on a second call like en.mysite.com/subsite realURL shows the postVar error.
the typoscript:
config {
sys_language_uid = 0
simulateStaticDocuments = 0
language = de
baseURL = http://fr.mysite.com/
}
[globalVar = GP:L = 1]||[globalVar = GP:L = en] || [globalString =
IENV:HTTP_HOST = en.mysite.com]
config {
sys_language_uid = 1
language = en
baseURL = http://en.mysite.com/
}
[global]
realurl_conf:
...
'preVars' =>
array (
0 => array(
'GETvar' => 'L',
'valueMap' => array(
'en' => '1'
),
'noMatch' => 'bypass'
)
),
...
Any help would be highly appreciated.
I don't think that changes in the default .htaccess are required but both domains are handled by TYPO3 and realurl.
In realurl you've to make a setup for both domains and adjust the required parameters on base of a default configuration.
In Backend you've to create the required language-records beside the setup which you posted.
Your Setup has a fault, for fr it should be like this:
config {
sys_language_uid = 0
simulateStaticDocuments = 0
language = fr
baseURL = http://fr.mysite.com/
}
EDIT: Above I assume that both domains are handled by the same TYPO3 installation. That's possible and in general a good solution.

Htaccess redirect different urls to redirect

I am writing some redirects because we're updating a website but we got to some trouble.
Here are some examples urls to redirect:
www.website.nl/location/depul.html -> www.website.nl/poppodium-de-pul
www.website.de/location/depul.html -> www.website.de/de_pul
www.website.nl/location/naturereserve.html -> www.website.nl/natuurgebied
www.website.de/location/naturereserve.html -> www.website.de/naturschutzgebiet
The different languages have different urls. So i have to redirect the .de url to the page on .de and the .nl url to the page on .nl
I was trying to use Redirect 301 but it seems impossible to use a full url in the to be redirected url.
Redirect 301 www.website.nl/location/depul.html www.website.nl/poppodium-de-pul
Does anyone know what i can do?
Thanks
Solved using php in my index.
Here is my code if anyone is interested.
$redirect_urls = array(
'/home.html' => array('','','',''),
'/hotelthing.html' => array('hotel','hotel','lhotel','hotel'),
'/hotelrooms/singleroom.html' => array('rooms/eenpersoonskamer','rooms/einzelzimmer','rooms/chambre_simple','rooms/single_room'),
'/hotelrooms/doubleroom.html' => array('rooms/tweepersoonskamer','rooms/doppelzimmer','rooms/chambre_double','rooms/double_room')
);
if (isset($redirect_urls[$_SERVER['REQUEST_URI']])){
$tld = strrchr ( $_SERVER['SERVER_NAME'], "." );
$tld = substr ( $tld, 1 );
if ($tld == 'nl') $sub = 0;
if ($tld == 'de') $sub = 1;
if ($tld == 'fr') $sub = 2;
if ($tld == 'com') $sub = 3;
header('Location: http://' . $_SERVER['SERVER_NAME'] . '/' . $redirect_urls[$_SERVER['REQUEST_URI']][$sub], true, 301);
exit();
}
Use this method:
Redirect 301 /location/depul.html www.website.nl/poppodium-de-pul

htaccess rewrite get string to integer

I want to receive a letter from a $_GET after making a post and rewriting an url with htaccess. For example:
$_POST['setting'] = 'w';
After a post I'm making a redirection like this:
/*more post variables here*/
$setting = $_POST['setting'];
header("Location{$basedir}page/$budget/$region/$reservations/$pool/$conference/$open/$meal/$setting/");
exit();
my htaccess configuration looks like this:
RewriteRule ^page/([0-9]+).([0-9]+)/([0-9]+).([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/[a-z]/?$ page.php?budget=$1.$2&region=$3.$4&reservations=$5&pool=$6&conference=$7&open=$8&meal=$9&setting=$10
I have tried with this too and had no luck:
RewriteRule ^page/([0-9]+).([0-9]+)/([0-9]+).([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/?$ page.php?budget=$1.$2&region=$3.$4&reservations=$5&pool=$6&conference=$7&open=$8&meal=$9&setting=$10
After the redirection $_GET['setting'] is not a string as I expect but an integer.
URL example: server/site/page/0.0/0.0/0/1/1/0/0/w/
This is how a var_dump of $_GET looks like.
array (size=8)
'budget' => string '15.30' (length=5)
'region' => string '0.0' (length=3)
'reservations' => string '0' (length=1)
'pool' => string '1' (length=1)
'conference' => string '0' (length=1)
'open' => string '1' (length=1)
'meal' => string '0' (length=1)
'setting' => string '150' (length=3)
Any idea of what is going on here or what am I doing wrong? Thank you
There is a limit of 9 back references in mod_rewrite, I read it from here RewriteRule using $10, $11, $12, and so on so a solution could be add the next references in a variable as a string and use the php explode function to catch the results.
try like that;
RewriteRule ^page/([0-9]+).([0-9]+)/([0-9]+).([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([a-zA-Z]+)/?$ page.php?budget=$1.$2&region=$3.$4&reservations=$5&pool=$6&conference=$7&open=$8&meal=$9&setting=$1

modrewrite for smart URL's | multiple variables?

What is the best way to accomplish that?
Im planning to do some kind of smart modrewrite + a function to grab variable name from the URL.
For example:
A URL like:
domain.com/page-blog/id-5/title-a_blog_title/date-2011_08_05
Would return:
$urlvariable = page-blog/id-5/title-a_blog_title/date-2011_08_05
Than, I will run a function that will parse the $urlvariable and return
$urlvariable['page'] = blog
$urlvariable['id'] = 5
$urlvariable['title'] = a_blog_title
$urlvariable['date'] = 2011_08_05
But the rewrite should be able to handle smaller or bigger urls like:
domain.com/page-blog/id-5/
returning:
$urlvariable = page-blog/id-5/
or also:
domain.com/page-blog/id-5/title-a_blog_title/date-2011_08_05/var1-foo/var2-bar/var3-xpto/var4-xyz etc ...
$urlvariable = page-blog/id-5/title-a_blog_title/date-2011_08_05/var1-foo/var2-bar/var3-xpto/var4-xyz
Any way to do that? What would be the expression for rewrite?
Thanks,
As for me, the best way is to route all request to your php/another_language script and then use something like this:
for($i=1,$arr=explode('/',$_SERVER['REQUEST_URI']),$s=count($arr);$i<$s;++$i){ //avoid 0(empty part) and 1(script name)
$tmp=explode(' ',$arr[$i]);
$get[$tmp[0]]=$tmp[1];
}
RiaD has the right idea, using explode rather than regexps (since regexps are slower).
However, you might want to use regular expressions anyway to "normalize" the URL. But after that you can still use explode for a tiny speed gain, or do something like this
$url = "page-blog/id-5/blank-/title-a_blog_title/date-2011_08_05/var1-foo/var2-bar/var3-xpto/var4-xyz";
$url = preg_replace("~//+~", '/', $url); // remove multiple consequtive slashes
$params = array();
if( preg_match_all("~(\w+)-([^/]*)~", $url, $params) ) {
$params = array_combine($params[1], $params[2]);
print_r($params);
}
That will print:
Array(
[page] => blog
[id] => 5
[blank] =>
[title] => a_blog_title
[date] => 2011_08_05
[var1] => foo
[var2] => bar
[var3] => xpto
[var4] => xyz
)

Resources