Mod rewrite, append directory as parameter - apache

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]

Related

Using Apache Rewrite Rule to pass information from URL to Filename

I've been banging my head against this one and have unfortunately failed. I'm hoping somebody can help me.
I'd like to pass information from a URL to a filename. Basically I'd like to do the following:
There is only one real file on the server:
http://www.domain.com/download/file.zip
If a user enters the following URL into his browser:
http://www.domain.com/download/file_xyx.zip I'd like apache internally serving file.zip but the user downloading the file as file_xyz.zip
xyz is intended to be a variable (of any length) so the rewrite rule should simply accept anything it's place and always internally link to the same file.
I'm thinking this can be achieved with a rewrite rule, am I wrong?
Yes, you just need this rule:
RewriteEngine On
RewriteRule ^/?download/file_[0-9]+\.zip /download/file.zip [L]
If you are using htaccess files, you can place this rule in an htaccess file in the "download" folder:
RewriteEngine On
RewriteBase /download/
RewriteRule ^file_[0-9]+\.zip file.zip [L]

htaccess mod_rewrite giving me 404

SO I have this in my .htaccess file
RewriteEngine On
RewriteBase /core/
RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
RewriteRule ^page page/%1/? [L]
my url is
http://localhost/core/page.php?page=8
with the rules applied I'm getting..
Not Found
The requested URL /core/page/8/ was not found on this server.
This is running on wampserver 2.2
the file structure looks like
c:/wamp/www/core
the .htaccess is inside the /core/ directory.
What is it that I'm missing.. i've checked my apache.conf file and it looks fine.
I think you got it the wrong way around. When logically thinking of rewriting you don't rewrite original URL to new URL (for example page.php?page=8 to page/8/) you actually rewrite page/8/ to page.php?page=8. You tell the server how it should interpret the unfamiliar URL.
So if I understood correctly what you want to achieve is:
User visits localhost/core/page/8/
User is served (under the hood) localhost/core/page.php?page=8
I believe the following RewriteRule will do the trick (The query string condition is not necessary):
RewriteRule ^page/(\d+)/$ page.php?page=$1 [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)

Using mod_rewrite correctly on Debian server

the good old mod_rewrite. I can't seem to get it right.
Typical scenario: A user types in "http://domain.com/page"
I want that the user is being redirected to "http://domain.com/page/page2"
My htaccess file looks as follows:
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^/page/$ page/page2
RewriteRule ^/bla/$ page/page2/bla
The first rewrite rule works, the second on the other hand doesn't seem to have any effect. Any idea? Maybe a better way to do this?
And another question:
As I said the first rewrite works just fine, but the url is not pretty. "http://domain.com/page" changes to "http://domain.com/page/page2". Is there a way to keep the typed in url but still forward the user to the actual link?
I presume the .htaccess is in your DocumentRoot.
How does your /bla containing look like? This should not rewrite the URL in the browser.
Use this:
RewriteEngine on
RewriteBase /
RewriteRule ^(/?)page/?$ $1page/page2 [L]
RewriteRule ^(/?)bla/?$ $1page/page2/bla [L]

apache rewrite url for codeigniter

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