I want to redirect the client from
http://mydomaine/cat?start=x
to
http://mydomaine/cat/page-(x divide by 15)
e.g from http://mydomaine/cat?start=30
to
http://mydomaine/cat/page-2
There any way to do that with .htaccess?
Not sure you can do this in .htacces file.
But, if you are under Joomla (from the tag of your question) you can create a system plugin which will let you manipulate urls.
Here is the doc from joomla.org Documentation
I think this tip only work with joomla 2.5.
Thanks all of you, so this the solution that is working for me:
if (preg_match("/(.*)\?limitstart=[0-9]+/", JRequest::getURI(), $matches)) {
header('Status: 301 Moved Permanently', false, 301);
header("Location: ".$matches[1]);
exit();
}
Related
I want to receive a parameter from URL with extension in that.
Here is my controller:
public function actionIndex($directory=null,$filename=null)
{
echo $directory.$filename;exit;
}
Here is my URL rule:
'<directory:\w+>/<filename:\w+>' => 'file/index',
it works like that:
localhost/uploads/abc
But it shows 404 when I do this:
localhost/uploads/abc.pdf
Any suggestions how this can be achieved?
I was able to achieve that with following regex:
'<directory:\w+>/<filename:\w+[.]\w+>' => 'file/index'
I have paginated URLs that look like http://www.domain.com/tag/apple/page/1/
If a URL such as http://www.domain.com/tag/apple/page/*2/ doesn't exist, or page/2 doesn't exist, I need code to redirect this to a page such as http://www.domain.com/tag/apple/ which would be the main tag page.
I've currently have the following code:
RewriteCond %{HTTP_HOST} !^http://www.domain.com/tag/([0-9a-zA-Z]*)/page/([0-9]*)/$
RewriteRule (.*) http://www.domain.com/tag/$1/ [R=301,L]
In this code, if the URL does not exist it redirects to the main tag page, but its not working.
Does anybody have an hints or solutions on how to solve this problem?
If I understand what you're saying, you are saying that you have a list of rewritten URLs (using mod_rewrite); some of them exist, some of them don't. With the ones that don't exist, you want them to be redirected to a new page location?
The short-answer is, you can't do that within htaccess. When you're working with mod_rewrite, your rewritten page names are passed to a controller file that translates the rewritten URL to what page/content it's supposed to display.
I'm only assuming you're using PHP, and if so, most PHP frameworks (CakePHP, Drupal, LithiumPHP, etc.) can take care of this issue for you and handle custom redirects for non-existing files. If you have a custom-written application, you'll need to handle the redirect within the PHP website and not in the .htaccess file.
A very simple example of this would be:
<?php
function getTag($url) {
if (preg_match('|/tag/([0-9a-zA-Z]*)/|', $url, $match)) {
return $match[1];
}
return '';
}
function validateUrl($url) {
if (preg_match('|/tag/([0-9a-zA-Z]*)/page/([0-9]*)/|', $url, $match)) {
$tag = $match[1];
$page = $match[2];
$isValid = // your code that checks if it's a URL/page that exists
return $isValid;
}
return false;
}
if (!validateUrl($_SERVER['REQUEST_URI'])) {
$tag = getTag($_SERVER['REQUEST_URI']);
header ('HTTP/1.1 301 Moved Permanently');
header('Location /tag/' . $tag . '/');
die();
}
?>
I am trying to route a URL using codeigniter URL routing.
I want to redirect a url like
/users/edit?email to userController/editemail
/users/edit?password to userController/editpassword
I tried using the following line in routes.php in config folder
$route["users/edit?(email|password)"] = "userController/edit$1";
This displays page not found. I am guessing that ? is being treated as a regular expression character. I tried escaping it but that didn't work either.
I don't want to set the config setting uri_protocol to PATH_INFO or QUERY_STRING, since this is just a pretty URL I want to setup, not pass anything to the action.
Can somebody help me out over here?
Regards
You should escape the ? like this, it should work. (not tested)
$route["users/edit\?(email|password)"] = "userController/edit$1";
Later edit:
This works as intended:
$route["users/edit(email|password)?"] = "userController/edit$1";
The userController looks like this
<?php
class UserController extends Controller {
function edit()
{
echo "general edit";
}
function editemail()
{
echo "edit email!";
}
function editpassword()
{
echo "edit password";
}
}
Router works like this:
if you go to http://sitename/index.php/users/editemail you see editemail() action.
if you go to http://sitename/index.php/users/editpassword you see editpassword() action.
if you go to http://sitename/index.php/users/edit you see the edit() action (the question mark makes optional the email/password field and you can do some other things in edit() action
From another forum I found the following example:
"I was looking for a way to pull node data via ajax and came up with the following solution for Drupal 6. After implementing the changes below, if you add ajax=1 in the URL (e.g. mysite.com/node/1?ajax=1), you'll get just the content and no page layout.
in the template.php file for your theme:
function phptemplate_preprocess_page(&$vars) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$vars['template_file'] = 'page-ajax';
}
}
then create page-ajax.tpl.php in your theme directory with this content:
<?php print $content; ?>
"
This seems like the logical way to do it and I did this, but the phptemplate_preprocess_page function is never called ... any suggestions?
I figured it out for myself from a Drupal Support Theme Development page:
"Maybe this helps
leahcim.2707 - May 29, 2008 - 05:40
I was trying to get the same thing done and for me this works, but I'm not sure if it is the correct way as I'm still new to Drupal:
in "template.php" I added the following function:
function phptemplate_preprocess_page(&$vars)
{
$css = $vars['css'];
unset($css['all']['module']['modules/system/system.css']);
unset($css['all']['module']['modules/system/defaults.css']);
$vars['styles'] = drupal_get_css($css);
}
I think after adding the function you need to go to /admin/build/themes so that Drupal recognises the function."
The part in bold is what did the trick ... you have to re-save the configuration so it recognizes that you've added a new function to the template.
How does the lighttpd rewrite work for folowing?
http://example.com/file_46634643.jpg to http://sub.domain.com/46634643.jpg
If it's possible...
Yes, it is possible. Use mod_rewrite, here is an example:
url.rewrite-once = ("^/file_([0-9]+\.jpg)$" => "/$1")
Check Lighttpd's mod_rewrite documentation for more details.
I don't think mod_rewrite allows you to "rewrite" to another (sub)domain. You could use mod_redirect (but it's a redirect, not a rewrite). Here is a slightly modified version of Bartosz's answer:
$HTTP["host"] == "example.com" {
url.redirect = ( "^/file_([0-9]+\.jpg)$" => "http://sub.domain.com/$1" )
}
Check http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRedirect