mod_rewrite not working as expected - mod-rewrite

i have an issue regarding mod_rewrite ... the url rewrite sems to work fine ... only thing is that the url in the address bar remains the same as it was before rewrite
for example: i want to rewrite www.site.com/page.php?page=merchandise to www.site.com/merchandise. now when i write the url i go to the right page (www.site.com/page.php?page=merchandise) but the address in the address bar remains ( www.site.com/page.php?page=merchandise) where it should be (www.site.com/merchandise) ... it works fine on the local environmnt...but problems occurs in live environment ...
I'm currently using simple RewriteRule.
The rewrite rule is as followed:
RewriteRule ^merchandise$ http://www.mysite.com/page.php?page=merchandise [NC]
Its pretty simple and should work. But it does not hide the actual address in the address bar. That's my problem.Otherwise it is going to right page.
Any help is appreciated.

You are describing the way mod_rewrite has been made for, and you are surprised that is works this way. This is funny :)
And it seems you're mixing things.
mod_rewrite is here to hide complex URLs and to make them simpler (generally speaking).
You are wrong when you say
"www.site.com/page.php?page=merchandise" to www.site.com/merchandise"
The real thing is probably the opposite:
you want the Net surfer to type "http://www.site.com/merchandise"
you want your server to change "http://www.site.com/merchandise" to "http://www.site.com/page.php?page=merchandise"
what you didn't get is that 99.9999% of the time webmasters don't want the client to see the real page is "http://www.site.com/page.php?page=merchandise"
...
And that's what does mod_rewrite: it looks at incoming URLs, and modify them internally. So the webdevelopper (= you) can transform the URLs using RewriteRule's and make whatever you want but it's always into the server. The only exception is when you want to explicitely redirect the client to another URLs, then you can use the "[F]" directive which means "Forward".
Hope this helps

Related

CodeIgniter router for joomla sef urls?

I wish to create a front-end for joomla using CodeIgniter. I prefer to make my own front-end that reads joomla database and I had my own "framework" to accomplish this, this is because I feel that joomla front-end is too heavy for high traffic websites, because it needs to accommodate for so many user needs, at the end you would really only use a small percentage of what joomla has to offer, yet a lot of unneeded core modules load and when you have a website with 300k visits per day, every bit of resource counts. And I like joomla because of its back-end and database structure.
Anyway, I'm looking to standardize a framework for joomla front-end using CodeIgniter instead of my own messy php code, the main issue for me is the ci router. In joomla I set sef url like these:
websitex.com/
websitex.com/category.html
websitex.com/category/subcat/123-alias.html
I'm looking for a router override that would allow me to process those urls. Basically, if any of the params has a number, then its for sure an article. If it doesn't, and its not index.php then its for sure a category or subcategory (if it exists).
And if I write this:
websitex.com/123 [OR]
websitex.com/category/123-alias.html
It would display the article with id 123. At least that's how it works in joomla and its what I'm trying to achieve here.
If anyone could point me in the right direction that would be great. Thanks in advance.
ADDED:
OK #ImranQamer comment left me thinking. What if I try to do it with conventional CI router? So I tried, and came up with this:
$route['default_controller'] = "controller/index";
$route['index'] = "controller/index";
$route[':num'] = "controller/article";
$route['(:any)/:num'] = "controller/article";
$route['(:any)'] = "controller/section";
I think it may work. I've run some tests and so far so good.
First and second line, if nothing is specified OR index.suffix (index.html) is the URI, then it goes to my controller index method.
Third line, if URI is a number (eg: /123.html) then it routes to controller/article.
Fourth line needs more testing, but apparently it will grab any URI that ends with a number (or number + suffix) and route it to controller/article. Still needs another rule to let article's title alias to be put in the URI (eg: /category/123-hello.html)
And last line, will treat the URI as one of the categories/section of the joomla site. In this controller though, I'm going to need to check if submitted URI corresponds to an existing section, in which case it gets displayed, otherwise, redirected to 404.
I'm gonna test this out for a while, but looks good so far.

mod_rewrite take part of url and add it to the end of a second url

I am attempting to make it where faculty or students at my library that have a pmid number for PubMed, and want to share the link with others could do it by just remembering our url, and adding the pmid to the end of our url, with the identifier p.
This is where mod_rewrite comes in it would drop off the url: site.com/p/112233444 but keep 112233444 and then add it to the end of linkresolver.com/112233444
The rule that I have come up with is:
RewriteRule ^/p/(.*)$ linkresolver.com=$1
First, is this possible I control the library domain, but I am not in control of the second url that I am attempting to add the PMID to.
Second, this is my first attempt at mod_rewrite so if I am way let me know I have looked at Apache's documentation. I know it is really powerful complex tool, so my rewrite rule just seems off.
Any help would be greatl
Does this work?
RewriteRule ^p/(.*)$ http://linkresolver.com/$1 [R]

ignoring last uri segment via mod_rewrite or CodeIgniter

I was just wondering if it is possible to ignore the last URI segment of my application via either mod_rewrite or CodeIgniter. I don't want a redirect away or a remove the URI segment. I just want my app to not know it exists. So in the browser the client will see:
http://example.com/keep/keep/ignore/
but the app is only aware of:
http://example.com/keep/keep/
The idea is, if JavaScript detects /ignore/ in the URI, it will trigger an action.
/ignore/ may appear as 1st, 2nd, 3rd or 4th segment, but will only ever appear as the final one and may sometimes not appear at all.
I found some info online about ignoring sub-directories with mod-rewrite, but none of them really work like this.
**
Incase any CodeIgniters suggest passing it as an unused extra parameter to my method - The app has far too many controllers and far too many wildcard routes for this to work site wide.
I think a mod_rewrite solution would be best if possible. If not, perhaps it can be done with a CodeIgniter pre-controller hook or something, but I'm not sure how that would work.
EDIT: How I got it to work
For anyone else who would ever like to know the same thing - in the end I overwrote _explode_segments() in MY_URI to not include this segment.
With the URI class you can check and detect what URI's are and what they have.
$this->uri->segment(n)
Check out the user guide: http://codeigniter.com/user_guide/libraries/uri.html

how does URL rewrite work in plain english

I have read a lot about URL rewriting but I still don't get it.
I understand that a URL like
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=19
can be replaced with a friendlier one like
http://www.example.com/Blog/2006/12/19/
and the server code can remain unchanged because there is some filter which transforms the new URL and sends it to the old, but does it replace the URLs in the HTML of the response too?
If the server code remains unchanged then it is possible that in my returned HTML code I have links like:
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=20
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=21
http://www.example.com/Blog/Posts.php?Year=2006&Month=12&Day=22
This defeats the purpose of having the nice URLs if in my page I still have the old ones.
Does URL rewriting (with a filter or something) also replace this content in the HTML?
Put another way... do the rewrite rules apply for the incoming request as well as the HTML content of the response?
Thank you!
The URL rewriter simply takes the incoming URL and if it matches a certain pattern it converts it to a URL that the server understands (assuming your rewrite rules are correct).
It does mean that a specific resource can be accessed multiple ways, but this does not "defeat the point", as the point is to have nice looking URLs, which you still do.
They do not rewrite the outgoing content, only the incoming URL.

How can I handle dynamic URIs where all segments are arbitrary?

How can push a URI like this in CodeIgniter (1.7.1 currently):
example.com/seg1/seg2/seg3/seg4/
or
example.com/seg1/seg2/
etc. through a single class method in a controller whose name does not appear in the URI? In a regular PHP scenario I would use mod_rewrite something like this:
RewriteRule ^([^/]+)/$ myfile.php?one=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2&three=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2&three=$3&four=$4 [L]
(I sanitize and validate the segments extensively in PHP, returning 404 if invalid)
But in Codeigniter I can't figure out how to do this without hard coding at least the first segment, but then using custom routes it still wants to treat the subsequent segments as method calls.
I'm a newbie to CI, but have so far managed to port over the entire existing site with the exception of this part of it. I don't see how all the parts come together on this problem, so any suggestion is welcome.
Clarification: I read through documentation on URI library, routing etc before starting my project, and they were helpful for various things, but this specific problem is not addressed by any of them. I'm not seeing how all segments of the URI can essentially be funneled through a controller that is not named in the URI and where all segments are arbitrary. The routing examples assume you know the value of the first segment. I already know how to remove "index.php" as well as access segments.
Have you looked up URI routing in the user guide and the wiki? They should tell you almost anything about routing, rewriting and accessing the different URI segments.
http://codeigniter.com/user_guide/general/routing.html
http://codeigniter.com/user_guide/libraries/uri.html
http://codeigniter.com/wiki/mod_rewrite/
[Edit:]
Here's the long description:
There is no way to not "hard-code" the first segment, and you can still "hard-code" the second segment.
What you want to accomplish can be nearly done by editing the routes in system/application/config/routes.php:
$route['(:any)'] = 'your_default_controller/index/$1';
$route['default_controller'] = "your_default_controller";
So, the first segment of your URI will be the method of the controller. You can access all segments of your (initial) URI by
$this->uri->segment(n)
You would then use the method index to call the desired function for each request.
On a side note: Why do you want to use an MVC framework for that, as you do not use much of the benefits of MVC?

Resources