htaccess: simulating a switch-case on urls especially 'default' - .htaccess

I have a site where I have to rewrite urls with htaccess. Is there a way to handle this like a switch's case statements?
Say my site is foobar.com, and I want
foobar.com/red => foobar.com/republican.php
foobar.com/blue => foobar.com/democrat.php
foobar.com/green/xyz => foobar.com/green.php?id=xyz
default:
foobar.com/* => foobar.com/page.php?query=*
Currently, this is my setup:
RewriteRule ^/red foobar.com/republican.php [works fine]
RewriteRule ^/blue foobar.com/democrat.php [works ok]
RewriteRule ^green/([^/.]+)/?$ green.php?id=$1
[error: redirect loop with foobar.com/green/home.php in browser address because green.php without id set redirects to home.php]
default [no idea how to implement]

Found a way of doing this:
See 'using a route file' on http://kehers.github.io/2014/07/07/url-rewrites-with-apache-and-php.html

Related

Usernames as subdomain

I have a multi-user website which generates user profiles.
By default, the profiles as set to the users' id, e.g. mysite.com/userid
I'd like to be able to change that to the username instead, and place it on the subdomain, e.g. username.mysite.com
Both the id and username are unique in mysql so there will be no duplicate issues.
But I'm struggling to find a way to do this.
I consulted this article: How to let PHP to create subdomain automatically for each user?
Which gave me some idea on how to start, but technically I'm lost.
I added the subdomain *.mysite.com, and tried the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^a-zA-Z0-9-]*)$ profile/?id=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.mysite.com
RewriteRule (.*) profile/?id=%1
Where "mysite.com" is my actual site. But it produced 404 errors.
Inside my index.php is the following line for seo profile pages:
$router->map(get_option('profile-seo-url','/profile/:name/:id/:section'), 'profile', array('methods' => 'GET,PUT,POST', 'filters' => array('id' => '(\d+)','section' => '(.*)')));
Is this overriding the htaccess file?

htaccess rewrite except one parameter

I want to make some url rewrite with htaccess,
http://www.example.com/article.php?post_id=123&title=2014-03-02-today-s-trends
=>
http://www.example.com/article/123/2014-03-02-today-s-trends
but some special rule. if some article click from rss or social share. I want to:
http://www.example.com/article.php?post_id=123&title=2014-03-02-today-s-trends&source=rss
=>
http://www.example.com/article/123/2014-03-02-today-s-trends?source=rss
my rewrite code here
RewriteRule ^article/(\d+)/(.*)\?source=(.*)?$ article.php?post_id=$1&title=$2&source=$3
but get 500 error. How to do it in a right way?
You can just use the QSA flag so that it will append the query string with it if source=rss is on the URL.
Not sure how your ogininal rewrite is but you can try this. It should work with or without source=rss
RewriteRule ^article/(.+)/(.+)$ article.php?post_id=$1&title=$2 [QSA,L]
Then in your code you can check for the source key in the $_GET.

Kohana 3.3 Routing Issue

This route I am using works perfectly on local MAMP server, but not Dreamhost or HostPapa. I figured it was just a case sensitivity issue, but from what I can tell everything looks fine.
Error Message
Kohana_HTTP_Exception [ 404 ]: The requested URL panel/asset/warranty/edit was not found on this server.
Route:
Route::set('panel/asset', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => 'panel/asset',
'controller' => 'warranty',
))
->defaults(array(
'action' => 'edit',
));
Controller: Controller/Panel/Asset/Warranty.php
class Controller_Panel_Asset_Warranty extends Controller_Site_AdminTemplate
.htaccess
# Turn on URL rewriting
RewriteEngine On
Options -MultiViews
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
SetEnv KOHANA_ENV DEVELOPMENT
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Required for dreamhost
RewriteCond $1 !^(index\.php|public|user_guide|robots\.txt)
RewriteRule .* index.php?$0 [PT,L,QSA]
Am I missing something obvious? Routing always gives me such grief... /annoyed
If you want a parameter to be a hard-coded string, do not put it in the regex array. Only use the regex array when you actually use some regex feature in the values.
This would be better. You could use the regex array to make sure id are digits, which is a valid reason to use it.
Route::set('panel/asset', 'panel/asset(/warranty(/<action>(/<id>)))')
->defaults(array(
'directory' => 'panel/asset',
'controller' => 'warranty',
'action' => 'edit',
));
This won't help you with the problem at hand though, if the URI haden't matched a route the exception read Kohana_HTTP_Exception [ 404 ]:Unable to find a route to match the URI: panel/asset/warranty/edit.
The exception is telling you Kohana is looking for a route to match the URI panel/asset/warranty/edit I assume that is what you want. So the problem would be somewhere in your application, not the .htaccess file.
There are only two methods which have the following block of code in it.
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $this->request->uri())
)->request($this->request);
Those methods are Request_Client_Internal::execute_request() and Controller::execute().
Have you placed some kind of debug statement in Controller/Panel/Asset/Warranty.php to see if it is found and executed? Because while reproducing I somehow the file was executed but class_exists('Controller_Panel_Asset_Warranty', FALSE) returned FALSE. When I copied the class name from your question and replaced the one I typed myself with it it suddenly worked. If I undid the paste it stopped working again. I checked letter for letter and my editor showed me the exact same thing before and after.
I hope this helps you narrow it down.

Mod-rewrite for domain except one directory

I am a novice at htaccess and can't figure out what's going on here. I am trying to get all redirects to point index.php with the PAGEID=pageName.
So, domain.com/manager would get pushed to index.php?PAGEID=manager.
Now, I have this working but I need it to just act normally when it hits the directory 'test', but whenever I goto domain.com/test I get pushed to index.php?PAGEID=test
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/test
RewriteRule ^([\w/]*)$ index.php?PAGEID=$1 [L]
Any ideas on how to get this working? What am I missing?
I think this logic is having some problems. Because in this example 'domain.com/test', we have write a condition as replace all strings(test) after domain.com to 'index.php?pageid=test'.
The problem is 'index.php' is also coming as a string after domain 'domain.com/index.php?pageid..' . Here also the rewrite condition will be applicable and it will rewrite it.
I think you should try something like this.
htaccess, redirect virtual subdomain to URL parameter
Or if 'test' is a static string, then in the rewrite condition we can specify as replace only string 'test' to 'index.php/pageid=test'

Yii and Mod Rewrite: redirect to user language translated url

I'm developing a multilanguage web app with Yii.
I applied changes to hide the index.php, changed urlFormat to path and added to the url path a slug with the user language example /it/index.php /en/index.php etc...
The problem now is that I need to redirect automatically to a different url once the user chooses another language. For example:
http://localhost/~antonio/project/it/women
needs to redirect to:
http://localhost/~antonio/project/it/femme
I have been playing with htaccess with no luck at all. Here is the actual code:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteBase /~antonio/project/
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
#My redirection code (tried a good few more to no use apart from this)
RewriteRule ^it/women$ it/femme
I would really appreciate any help on this issue, as it is driving me mad.
Thanks
Edit::
I surrendered with mod_rewrite. I found another solution by adding this code to /layout/main.php:
<?php
$onurl = Yii::app()->getRequest()->requestUri;
if ($onurl == "/~antonio/project/it/women") {
$this->redirect("/~antonio/project/it/femme");
} elseif ($onurl == "/~antonio/project/it/men") {
$this->redirect("/~antonio/project/it/uomme");
}
Rinse and repeat per combination of language/word
This might not work without setting up a proper Virtual Host (so that instead of local urls like http://localhost/~antonio/project/it/women you have nice urls like http://project1.dev). But I would do that anyway, since it makes your dev environment nicer! (Here's a place to start).
Anyway, I would try this: just leave the .htaccess file set like you normally would for "path" style urls, and then just parse a $_GET['lang'] parameter using the UrlManager? So you would have a urlManager setup like this in your config.php:
'urlManager'=>array(
'urlFormat'=>'path', // use path style parameters
'showScriptName'=>false, // get rid of index.php
'rules'=>array(
// this parses out the first chunk of url into the "lang" GET parameter
'<lang:[\w\-]+>/<controller:[\w\-]+>/<action:[\w\-]+>'=>'<controller>/<action>',
)
)
This way a url like this http://project1.dev/it/controller/action will redirect to the "action" action in your Controller like normal (controller/action), but in your action $_GET['lang'] will now have the value of "it".
I hope that helps, it's kind of a shot in the dark. I'm not sure how you are actually triggering the different languages, so a $_GET parameter might not be helpful after all.
Found a way to do this in htaccess:
RewriteCond %{REQUEST_URI} ^/~antonio/project/it/donna/shoes/(.*)$
RewriteRule ^it/donna/shoes/(.*)$ /~antonio/project/it/donna/calzature/$1 [L,R]

Resources