Htaccess redirect different urls to redirect - .htaccess

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

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;
}

Rewrite Query string in URL using .htaccess

How to rewrite my website's url from "website.com/example.php?site=youtube" to "website.com/example/youtube" where site is a variable?
Should i change the url in the php file too?
You can use this in your .htaccess file:
RewriteEngine On
RewriteRule ^example/([^/]*)$ /example.php?site=$1 [L]
That will leave you with the following URL: http://website.com/example/youtube. Make sure you clear your cache before you test this.
$connection = mysqli_connect("localhost" , "root" , "" , "test");
$query = "SELECT * FROM websites";
$select_all_sites = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_all_sites)) {
$site_title = $row['site_title'];
echo "<a class='item' href='site.php?website=$site_title'> $site_title</a>";
On the other page which is called site.php i am using this code to get the variable from the sites.php page
function myname(){
if(isset($_GET['website'])){
$variable = $_GET['website'];
echo $variable;
}
}

Opencart Search page as SEO url

Trying to get my search results page SEO-friendly .. seems so basic, yet my tags rewrite rule is conflicting it.
(Which also uses the product/search router) -- disabling the tags rewrite, search still doesn't seem to work properly.. it shows whatever it wants, even if I search a nonexistent item. (This behavior doesn't occur under the normal index.php?route=product/search URL)
.htaccess:
RewriteRule ^tags/([^/]*)$ index.php?route=product/search&tag=%{QUERY_STRING} [L]
RewriteRule ^search/([^/]*)$ index.php?route=product/search&search=%{QUERY_STRING} [L]
catalog/controller/startup/seo_url.php:
} elseif ($data['route'] == 'product/search' && $key == 'tag') {
$url .= '/tags/' . str_replace(' ','-',$value);
unset($data[$key]);
} elseif ($data['route'] == 'product/search' && $key !== 'tag') {
$url .= '/search/' . str_replace(' ','-',$value);
unset($data[$key]);
//....
Is there any way to rewrite both of these routes without choosing one or the other?
Using Opencart 2.3.0
catalog/controller/startup/seo_url.php
find:
$this->request->get['route'] = 'error/not_found';
add:
if (strpos($this->request->get['_route_'], 'tag/') !== false) {
$this->request->get['route'] = 'product/search';
$this->request->get['tag'] = str_replace('tag/','',$this->request->get['_route_']);
}
elseif (strpos($this->request->get['_route_'], 'search/') !== false) {
$this->request->get['route'] = 'product/search';
$this->request->get['search'] = str_replace('search/','',$this->request->get['_route_']);
}
else {
$this->request->get['route'] = 'error/not_found';
}
.htaccess:
RewriteRule ^tag/([^/]*)$ index.php?route=product/search&tag=$1 [L]
RewriteRule ^search/(.*)$ index.php?route=product/search&search=$1 [L]

How do I fix SEO urls with lighttpd?

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

How do I rewrite a name to an id?

I'm trying to rewrite the following URL
www.domain.com/category.php?id=13
to:
www.domain.com/category/cat-name
I've followed some .htaccess tutorials but I couldn't. How can I make it work?
You cannot rewrite a number to a name, unless you have some way of translating that number to a name. This means that on the http deamon level (Apache/.htaccess) you don't have enough information to redirect it. For redirects in php, see this.
Since you ask about rewrites in php, I'll answer that too. There is no such thing as rewriting in a php file. An internal rewrite maps one url (e.g. www.domain.com/category/cat-name) and lets the server execute a different url (e.g. www.domain.com/category.php?id=13). This happens on the http deamon level, in your case Apache.
First make a .htaccess in your www-root:
RewriteEngine on
RewriteRule ^category/[^/]+$ /category.php
In category.php:
if( strpos( $_SERVER['REQUEST_URI'], 'category.php' ) != FALSE ) {
//Non-seo url, let's redirect them
$name = getSeoNameForId( $_GET['id'] );
if( $name ) {
header( "Location: http://www.domain.com/category/$name" );
exit();
} else {
//Invalid or non-existing id?
die( "Bad request" );
}
}
$uri = explode( '/', $_SERVER['REQUEST_URI'] );
if( count( $uri ) > 1 ) {
$name = $uri[1];
} else {
//Requested as http://www.domain.com/category ?
die( "Bad request" );
}
This will redirect requests to category/name if you request category.php?id=number and get the name from the url if you request category/name

Resources