mod_rewrite handling extra GET variables - mod-rewrite

I have a mod rewrite rule for pagination of my content:
RewriteRule ^content/page/([^/]*)$ /index.php?page=$1 [L]
However, I am also attempting to handle facebook connect, which will result in a url that looks like this:
http://content.local/page/2?session={"session_key"...
However, with my current rewrite rule, the session variable does not get passed along. How can I pass that variable in addition? I would want to un-rewritten link to look something like /index.php?page=2&session={"session_key"...

Use the QSA flag like this:
RewriteRule ^content/page/([^/]*)$ /index.php?page=$1 [QSA,L]

Related

mod_rewrite separate two numbers with hyphen/dash

To secure my URL I am trying to add some additional numeric values to the user's id using mod_rewrite.
I have this URL
https://www.domain.com/dashboard.php?userempid=48&hgid=45
I need to rewrite the above URL to
https://www.domain.com/1848-6245.html
This is what I tried and it never works
RewriteRule ^18([0-9])-62([0-9]).html$ dashboard.php?userempid=$1&hgid=$2 [NC,L]
Please Help!
Change your rule adding a plus:
RewriteRule ^18([0-9]+)\-62([0-9]+).html$ dashboard.php?userempid=$1&hgid=$2 [NC,L]

Rewriting url with the following regular expression

I'm trying to do a mod rewrite to get this url: localhost/test/index.php?hello
I created a file for this page called hello.php and it is in the folder /test
To clarify, I have another page that has a link to my hello.php, but what is the correct url so I can display localhost/test/index.php?hello in the url when I click the link to access my hello.php page.
The following doesn't seem like it is right:
RewriteRule ^.*$ index.php?$1 [L]
Try this if you want to just do php files.
RewriteEngine on
RewriteRule (.*)\.php$ /index.php?$1 [L]
To clarify what my answer does. It gives you more friendly URLs which it sounded like what your were asking for.
So you can use localhost/hello.php and it will be internally redirect to /localhost/index.php?hello. Internally means they will never see localhost/index.php?hello and will always see localhost/hello.php in their browser.
You can do any URL and it will rewrite to a php file. e.g. localhost/index.php?newpage and you can use /localhost/newpage.php
Hope that is clearer.
EDIT
You want the reverse but I don't know how your PHP is constructed but query strings are typically field/value pairs. For example name=john, page=contact, action=hello etc. You have index.php?hello but hello has no content or value.
That's probably why you're having such a hard time even re-writing your URL. Using $_GETwould require a value.
So what I would do, is if your URL was like this using field/value pairs
index.php?action=hello
Then in the index.php file you could do something like
$action = $_GET["action"];
if($action == "hello"){
//show contents of hello, include a page or whatever
}
Once you have good URLs it would be easy to rewrite it.
So if the URL that you want shown is like
index.php?action=hello and you want to redirect it to hello.php
Your .htaccess would look like this
RewriteRule ^action=([^/]+) /$1.php [R,L]
That would redirect it to the php file. If you don't want to show the redirection and keep it an internal redirect you can remove the R flag and just keep [L].
I personally don't want the user to see really long query strings URL example. mysite.com?page=music&artist=someartist&title=sometitle
So all my URL's are rewritten to be shorter and friendlier like my original answer.
you don't need .htaccess for 2. as far as you're using GET parametr - use it in index.php:
if (isset($_GET['hello'])) include('hello.php');
this will show the contents of hello.php inside index.php

Mod_rewrite - don't display certain query string in the url

I know this question may have been asked several times, but to be honest I haven't yet found a complete answer for this.
I have this url:
modelDetails.php?manufacturerName=$1&manfuacturerID=$2&modelName=$3&modelID=$4&yachtCode=$5&lang=$6
Is it possible not to display yachtCode and lang in the url and still pass the values from page to page?
This is my htacces file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteCond %{QUERY_STRING} ^lang=(EN|DE|NL)$ [NC]
RewriteRule ^(.*)/([0-9]+)/(.*)/([0-9]+)/([0-9]+)$ modelDetails.php?manufacturerName=$1&manufacturerID=$2&modelName=$3&modelID=$4&yachtCode=$5 [L,QSA]
If you remove the data from the query string, then the only other way to access the data is to retrieve it from a cookie. But to set the cookie you'll have to have the yachtCode value appear in the query string at some point.
The only alternative would be to use POST (the mode used by a web form) instead of GET (the mode which is generally used). POST mode submits variables as part of the request, rather than adding the variables to the query string. But you can't force a hyperlink to use POST mode, so this is probably not of use to you.
In short, you probably can't hide yachtCode from the end user completely.

Rewrite Condition not working for Rewrite rule on individual pages

I am trying to write a rule that will capture any url that does NOT have sales/anything up to a .php or .php3 file and anything after that - if there is anything - and rewrite that to a new website as per below:
RewriteCond %{REQUEST_URI} !^(/sales/.*php3?).*
RewriteRule ^/sales/([^./]*)$ http://www2.domain.com/sales$1/index.shtml [R,L]
It captures if I put in www.domain.com/sales but if I put in just http://www.domain.com/sales/trucks.shtml if does not capture the individual pages.
Can anyone see what I need to do to get this to work correctly please ?
To clarify:
.If I put in url www.domain.com/sales, the site redirects to www2.domain.com/sales/index.shtml ....however if I put in the url www.domain.com/sales/trucks.shtml the condition is not picked up and the url does not rewrite to the ww2 site so I am stuck on the old page still ....thanks for your help
Alright use these 2 rules for your requirements:
RewriteRule ^sales/?$ http://www2.domain.com/sales/index.shtml [R,L,NC]
RewriteRule ^sales/(?!.*\.php3?$).*$ http://www2.domain.com%{REQUEST_URI} [R,L,NC]

Getting the original REQUEST_URI when using mod_rewrite AND mod_proxy

I'm using a custom.conf file for rewrites and codeigniter for some features of the site, mainly the articles.
My original url gets rewritten, so I have http://example.com/article-a101, this uses the custom.conf file to rewrite to codeigniter/article/read/101. I think I must send this as a proxy call using the [P] flag in mod_rewrite to make it rewrite again in codeigniters .htaccess file. Once it hits the code igniter .htaccess, it uses that mod rewrite structure to call the index file and use the article controller and the read function sending in the 101 as the parameter.
What I'm trying to figure it is how do I get the original url in the address bar as its not in the $_SERVER variable. Since I use the [P] on the first rewrite, request_uri has codeigniter/article/read/101.
custom.conf
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2 [P,L]
codeigniters .htaccess, fairly basic
RewriteRule ^(.*)$ index.php?/$1 [L]
Here's my current solution that I know there must be a better method for
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2?orig_url=%{REQUEST_URI}&%{QUERY_STRING} [P,L]
This stays hidden from the user, and I can access the original url through the query string, but doesn't seem like an elegant solution.
I'm pretty sure you cant do it any other way with mod_rewrite
but you could do it with codeigniter routing.
$route['^([_a-zA-Z0-9-]+)-a([0-9]+)$'] = "article/read/$2";
assuming your controller is named article and your function is named read
if you visited /article-a101
then $this->uri->uri_string(); would return article-a101 (the original url, which should be in your url bar now)
and $this->uri->ruri_string(); would return article/read/101 (where you actually are)

Resources