apache rewrite url for codeigniter - apache

I need to build app and i'm using codeigniter for this.
Company sends me a link in format that cant be changed from their side
Link is like:
someurl.com/sms.php?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper& mssid=1234567892&notcharged=0&date=2011-12-26+23%3A31%3A27&keyword=msg&created=2011-12-26+23%3A31%3A26
How can i rewrite it with .htaccess so the codeigniter gets link in segmented format like
someurl.com/sms/myfunction/12345678/msg15/777/cc/someoper/1234567892/0/2011-12-26+23%3A31%3A27/msg/2011-12-26+23%3A31%3A26
Thanks
Edit.
Spent some hours and tried something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^phone\=([^&]+)\&msg\=([^&]+)\&code\=([^&]+)\&country\=([^&]+)\&oper\=([^&]+)\&mssid\=([^&]+)\&date\=([^&]+)\&keyword\=([^&]+)$
RewriteRule ^test\.php$ /sms/doParse/%1/%2/%3/%4/%5/%6/%7/%8 [R,L]
And then typing url like this:
http://test.airtel.lv/test.php?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper&mssid=1234567892&date=2011-12-26+23%3A31%3A27&keyword=msg
I got this:
http://test.airtel.lv/sms/doParse/12345678/msg15/777/cc/someoper/1234567892/2011-12-26+23%253A31%253A27/msg?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper&mssid=1234567892&date=2011-12-26+23%253A31%253A27&keyword=msg
Why returned url has GET params in the ending ?
And one more question, if that company will change and add extra param to link, the rewriting will be broken ? Then how can it be more universal ?

Try this placing this in your .htaccess file in an appropriate place in your document root:
RewriteEngine on
RewriteRule ^sms.php$ /sms/myfunction/ [L]
RewriteCond %{QUERY_STRING} ^[^=]+=([^&]+)&?(.*)
RewriteRule ^sms/myfunction/(.*)$ /sms/myfunction/$1/%1?%2 [L]
The first RewriteRule changes the /sms.php part of the URI to /sms/myfunction/ and the second rule will append each value in your query string into its own part of the path. So a url like this: http://someurl/sms.php?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9 will have the URI internally rewritten to /sms/myfunction/1/2/3/4/5/6/7/8/9. There's no checks on what the variable names are in the query string, only the value is extracted and appended to the URI path.
If you put that code in your server or vhost config (instead of an htaccess file) add a / in front of the sms in each of the rules so it says ^/sms because rules in the htaccess have the leading slash stripped off when matching against the URI.

just handle the GETs in your controller. it's okay, GET isnt evil.
parse_str($_SERVER['QUERY_STRING'], $_GET);
then you can have a simpler rewrite to handle the special route

Related

Redirect from PHP file to just query string

I want to use mod rewrite to redirect the following dynamic URL with h query string:
https://eu1.domain.com/~username/test.php?h=String_112016
To the following URL without the test.php part:
https://eu1.domain.com/~username/String_112016
I tried the following in my htaccess file but it's getting 404:
RewriteEngine On
RewriteRule ^\~username/([^/]*)$ /~username/test.php?h=$1 [L]
I don't have any other rule in the htaccess file.
From the code you wrote, I'm assuming what you want is not actually a redirect. Correct me if I'm wrong, but what you want is that when a user requests /~username/String_112016, you want the test.php page to be rendered and passed "String_112016" as a parameter labeled h.
You're really really close... Try this:
RewriteEngine On
RewriteRule ^/?~username/([^/]*)$ /~username/test.php?h=$1 [L]
The first matched character in the Rewrite Rule should be a forward slash, not a backslash. This forward slash is practically always present in requests, but I think it's best practice to treat it as optional, because technically you could get a (malformed) request that does not have one. The question mark after the /, makes the / optional.
RewriteEngine On
RewriteBase /
RewriteRule ^~username/test.php\?h=(.*)$ /~username/$1 [L]

Mod rewrite, append directory as parameter

I'm wondering how to do directory rewriting where the directory gets appended as a parameter regardless of the remaining url.
For example, if I have urls like these:
example.com/peter/lois/stewie.php
example.com/peter/lois/brian.php?drinks=martinis
example.com/peter/lois/quagmire/meg.php
and I need them to rewrite to
example.com/lois/stewie.php?person=peter
example.com/lois/brian.php?drinks=martinis&person=peter
example.com/lois/quagmire/meg.php?person=peter
how do I go about doing that?
Thanks!
EDIT
So I have it implemented like this in the sites-available file:
RewriteRule ^/([a-z0-9]+)/lois(.*) /lois$2?person=$1 [QSA,L]
but instead of returning like
example.com/lois/stewie.php?person=peter
it's returning like
example.com/peter/lois/stewie.php?person=peter
So it's putting the parameter at the end of the url like it should, but it's not dropping the earlier directory.
mod_rewrite would work well for this:
RewriteEngine on
RewriteRule ^([a-z0-9]+)/(.*\.php) /$2?person=$1 [QSA,L]

Rewrite pages with a certain url to another htaccess

What I need to do it rewrite a url from one thing to another eg:
http://www.domain.com/page_one/blah //to
http://www.domain.com/page_two/blah
I've tried a few script I've found around the internet but I'm terrible with .htaccess and can never understand or get it right.
If you want to change only this specific url, use this:
RewriteEngine On
RewriteRule ^page_one/blah?$ http://www.domain.com/page_two/bla [L]
If you want to change the url for every file into the "page_one"-folder, this will help you:
RewriteEngine On
RewriteRule ^page_one/([^/]+)$ http://www.domain.com/page_two/$1 [L]
The RewriteEngine On activates the RewriteEnigine, so that you are able to use RewriteRules.
The ([^/]+) in the second solution means "every file, but no folders (the slash is excluded)". This is stored in $1 and used to create the new url.
Edit
[L] stops the script from using the other rules (if you have some)

htaccess rewrite drive me nuts

I want to use a rather simple rewrite, something like this:
RewriteRule monitor.html index.php/\?first_category_id=B008 [NC,L]
But it doesn't work as expected, goes to like index.php/monitor.html (which kicks in symfony's routing and returns a 404 error but this is a different story)
However if i include full url like:
RewriteRule monitor.html http://example.com/index.php/\?first_category_id=B008 [NC,L]
it responses the correct content, but this looks like a full redirect, the rewrited url is revealed in the browser. And thats not transparent nor easily deployable.
What am i missing here?
the rest of the htaccess file if it matters:
RewriteCond %{REQUEST_URI} \..+$
RewriteRule .* - [L]
RewriteRule ^(.*)$ index.php [QSA,L]
Your rule is outputting a relative path and you're in a per-directory context. You need RewriteBase. In a per-directory context, rewriting is being done on expanded filesystem paths, not on the original URL's. But the results of the expansion are converted to a URL again! RewriteBase supplies the prefix needed to do that. Without it, the URL is naively made out of the same filesystem prefix that was stripped prior to the substitution and you end up with for instance http://example.com/var/www/docroot/blah... which is nonsense. Either RewriteBase or put out an absolute, beginning with a slash.
Also, you should anchor the match:
RewriteRule ^monitor.html$ ...
Otherwise the rule will potentially match somewhere in the middle of the path and just that matching part will be replaced with the substitution! You don't want to match and translate amonitor.htmly/foobar, right, and convert just the monitor.html part to a the index.php stuff.
You should not escape the question mark in the substitution. It's not a regexp! Just index.php/?etc not index.php/\?etc (Could that backslash be what is screwing up, causing `index.php/monitor.html'?)

Apache Mod-Rewrite Question

I have a PHP scripted named index.php inside a folder named blog. There are three different views.
http://www.myDomain.com/blog/index.php
http://www.myDomain.com/blog/index.php?tags=list of categories
http://www.myDomain.com/blog/index.php?post=name of post
I would like to change the view based on the URL.
/blog redirects to number 1 above
/blog/name-of-category redirects to numbe 2 above
/blog/name-of-category/name-of-post redirects to number 3 above.
Right now I have the following mod_rewrite rules.
RewriteRule ^blog$ blog/index.php [L]
RewriteRule ^blog/(.+)/(.+)$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+)$ blog/index.php?tags=$1 [L]
This does not work, and I'm not sure why. Right now it always redirects to the last URL:
blog/index.php?tags=$1
And the GET data contains "index.php."
Also, if add a forward slash to the final rule like so:
RewriteRule ^blog/(.+)/$ blog/index.php?tags=$1 [L]
All redirects work fine. The problem is, I'm required to have a forward slash at the end of the URL if I want the category view.
Any ideas what's happening here? how I can fix this?
Thanks for the replies. I figured out that my problem was a side effect of having my scripts inside the folder named "blog". Here's what index.php looked like:
<?php
define ('BASE_PATH', "../blog/");
include_once(BASE_PATH . 'controller/Controller.php');
$controller = new Controller();
$controller->invoke();
See the problem? Because my script's base path was "blog", mod_rewrite was rewriting all my references inside the program. By renaming my script folder to blogScript, it fixed the problem.
In a regular expression, . matches any character (including a / character) so try doing ^blog/([^/]+)$ instead to match any character except a /.
You could write it as follows.
RewriteRule ^blog/?$ blog/index.php [L]
RewriteRule ^blog/(.+?)/(.+?)/?$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+?)/?$ blog/index.php?tags=$1 [L]