Codeigniter 2.1 and .htaccess - rewrite url - .htaccess

I need to rewrite this url:
domain.com/mali_oglasi/index/1(any number)
to:
domain.com/mali_oglasi
In my .htaccess file I have this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
How can I do this?

If the only thing you want is to map your controller/method differently than the default behaviour, you can use the route.php config file. See the official documentation here : http://codeigniter.com/user_guide/general/routing.html
In your case you'll have something like this :
$route['mali_oglasi/index/(:num)'] = 'mali_oglasi';
Later in your controller you can still get the original digit by using :
$this->uri->rsegment(3);
instead of :
$this->uri->segment(3);
(see official documentation here : http://codeigniter.com/user_guide/libraries/uri.html )
EDIT:
In fact, if you just wish to get rid of the "index" segment when you need to add parameter, you may want to do the inverse of my first answer :
$route['mali_oglasi/(:num)'] = 'mali_oglasi/index/$1';
With that line, every request in the form of "www.yourdomain.com/mali_oglasi/1" will be interpreted by codeigniter as if it were "www.yourdomain.com/mali_oglasi/index/1". Meaning the method "index" of the controller "mali_oglasi" will be used to handle your request.
If you need to retrieve the digit, you want to use :
$this->uri->segment(3);
So if your client should ever go to the url "www.yourdomain.com/mali_oglasi/index/1" directly, you will still retrieve the good uri segment. ( $this->uri->segment(n); give you the n-th segment after route.php rewrite the uri, and $this->uri->rsegment(n) give you the n'th segment before the uri is rewritten. )

I suggest to redirect the user to the new URL :
in your controller mali_oglasi >> in the function index
put the below line
redirect('mali_oglasi');
e.g.
class mali_oglasi extends CI_Controller{
function Index($id){
// Note : make sure you have loaded the url helper
redirect('mali_oglasi');
}
}
Note: don't forget to load the url helper
Note: Set the $config['index_page'] = ''; instead of index in application/config/config.php

Related

get specific part of url using regex

I have a URL
ws://mydomain.com/auth/mZFZN4yc/?rtmpUrl=rtmp://abc.com/live/0q4wwjye
How can i get the auth string "mZFZN4yc" from this.
I have used in NodeJS
req.url.match(/^\/auth\/(.*)$/)
but it is returning this whole part
mZFZN4yc/?rtmpUrl=rtmp://abc.com/live/0q4wwjye
Instead of .* you can use [^\/]+, so you get it all up to the /, like:
req.url.match(/\/auth\/([^\/]+)/)

GAE Application app.yaml VS .htaccess

How to Write a app yaml looks like htacess below
RewriteEngine on
# To append a query string part in the substitution string
RewriteRule ^([0-9a-z_/\-]+)/$ index.php\?p=$1 [QSA]
RewriteRule ^([0-9a-z_/\-]+)$ index.php\?p=$1 [QSA]
im doing so at app yaml for GAE Application was fail
as Dan mentioned, you will not be able to handle this all in the yaml, and will need to to handle the logic yourself, we do a simular thing in one of our project and will outline below our solution.
Our scenario is handling the old website article's URL structure, and trying to redirect them to the new URL structure.
In our yaml we register the pattern that we are looking to match on and direct it to a file where we will do the handling :
- url: (/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/.*) (Pattern to match on)
script: publication.custom.redirector.app (Path to your .py that will have your handling in)
In our .py file we will catch that pattern and route it to our DefaultHandler that can then do any logic you need and redirect out:
( in our project this goes to /publication/custom/redirector.py )
import request
import settings
import re
class DefaultHandler(request.handler):
def get(self, pre, year, month, day, post):
post = re.sub('(.*[^0-9])[\d]{1}$', r'\1', post)
post = re.sub('[^0-9a-zA-Z-_\/]+', '', post)
path = post.split("/")[-1]
slug = "{0}-{1}-{2}-{3}".format(year, month, day, path)
article = self.context.call('pub/articles/get', slug=slug.lower())
if article:
self.redirect(article['pub_url'], permanent=True)
else:
self.render("pages/page-not-found/page-not-found.html")
app = request.app([
('/(.*)/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)', DefaultHandler)
], settings.gaext.config)
Hope this helps
The GAE app.yaml doesn't have a URL rewrite capability, it just parses the incoming request URL for request routing purposes, to determine which handlers to invoke.
One could maybe argue that the static_file handlers configuration has a somewhat similar capability, but it is only applicable to the static assets.
For the dynamic handlers you'd need to take care of such "rewrite" inside your app code. I'm quoting "rewrite" here as technically it's just a different way of parsing/interpreting the request URL inside your app code - the original, unchanged request URL will still be the one recorded by the GAE infra.

Pass parameter to default controller/method with Yii urlManager

I would like to use a catch-all rule for urlManager that would pass anything after my base url to a default controller and method as parameters.
My goal would be for a url such as mysite.com/123 to map to mysite.com/controller/method/123 where controller/method are predetermined and 123 is passed as a named parameter.
Such a rule would be put last in the urlManager chain so that if none of the other rules match it would pass whatever is after the base url to my selected controller/method.
Any ideas??
Edit:
Adding a rule '<id>'=>'controller/method' (which I think I had tried anyhow) and then viewing site.com/123 would return a 404 not found, but from apache, NOT Yii. Something I did not take into consideration.
Going to mysite.com/index.php/123 got the desired result. Going to mysite.com/controller/method though would route the url properly. Strange...
Yes, you have to put this as the last rule under all other rules.
'<id>' => 'controllerName/methodName/<id>,'
Example:
'<id>' => 'user/view/<id>',
This will redirect all URLs like this:
mysite.com/1
To:
mysite.com/user/view/1
If you want to restrict to numbers only, use
'<id:\d+>' => 'controllerName/methodName/<id>,'
You should add this rule to bottom of url rules:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<pname:\w+>'=>'site/test',
),
),
Pname: your named parameter.
Site/test: the target action.
In your action you should define your "pname" as method paramter:
public function actionTest($pname) {
echo "Name:$pname";
}

htaccess mod-rewrite anchor [duplicate]

This question already has answers here:
'hash' url rewrite in .htaccess
(2 answers)
Closed 8 years ago.
I have this url: mysite.com/account/user#editSucc, I want it to be parsed as mysite.com?goTo=account&section=user&msg=editSucc.
This is my .htaccess file:
RewriteEngine On
RewriteRule ^account/([A-Za-z-]+)$ /?goTo=account&section=$1 [L,NC]
How can I make the part with the # symbol? and merge it with the existing code?
Thanks.
You can't do this using htaccess. The URL fragment (everything after #) is never sent to the server and thus nothing on the server's end even knows it exists. You need a strictly client side solution to deal with it. Since you want it to be redirected (and that's all you'll be able to do with it). you can try something like from this link:
<script type="text/javascript">
var parts = location.href.split('#');
if(parts.length > 1)
{
location.href = '/?goTo=account&section=user&msg=' + parts[1];
}

HTACCESS - Block everything but specified SEO friendly URL

I haven't found all the answer to my current problem.
Here is the root of the site:
cache
img
display.php
admin.php
What I need is to block all the direct access of the files and allow only access via url formatted like that:
1 ht*p://sub.domain.com/image/param/size/folder/img.jpg (param, size, folder, img are parameters)
2 ht*p://sub.domain.com/action/param1/param2/ (param1, param2 are parameters)
1 would point to display.php with the correct parameters
2 would point to admin.php with the correct parameters
Every other access must be 404 (at best) or 403
my rules are (the htaccess is in ht*p://sub.domain.com/):
RewriteRule ^image/([^/]+)/([0-9]+)/([^/]+)/([^/]+)\.jpg display.php?param=$1&size=$2&folder=$3&img=$4 [L]
RewriteRule ^action/([^/]+)/([^/]+) admin.php?action=$1&param=$2 [L]
Those rules work as I want to but I am stuck on how to block any access that does not come from those URL!
Also (as a bonus) I would like to be able to use the same htaccess on diferrent web address without having to change this file.
Thanks in advance
Have you try moving the image out of the public folder and use php to call the image in?
For the PHP files you can use the switch statement (http://www.php.net/switch).
For the admin.php file you can do something like:
$get_action = $_GET['action'];
switch ($get_action) {
case "edit":
case "view":
case "delete":
case "add":
//Continue loading the page
break;
default:
header('HTTP/1.1 403 Forbidden');
die();
}
Note: I don't know how your code looks or works, but you can have an idea base on the code I added.

Resources