Can htaccess override CodeIgniter's routes.php - .htaccess

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

Related

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.

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]

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

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