Rewriting url with the following regular expression - mod-rewrite

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

Related

Joomla htaccess rewrite url - Parameter must index by a number - Why?

this is the firsttime a put a question here, so dont hard on me. Thank you.
I currently setup a joomla site. I create a page, and a new template, and a module, inside the template/index.php i call my module.
The original url that works is something like:
index.php/danh-sach-game?gt_name=game_mang_xa_hoi
danh-sach-game: is the page.
game-mang-xa-hoi: is the input parameter to the module.
everythings works find but i want to rewrite url to this:
danh-sach-game/game-mang-xa-hoi
So i created a .htaccess with content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^danh-sach-game/(.*)$ index.php/danh-sach-game?gt_name=$1 [L]
</IfModule>
Now this is time for "MAGIC"
if i enter the url:
danh-sach-game/game-mang-xa-hoi
then Joomla push a message "An error has occurred.The requested page cannot be found."
But if i index the parameter by a number like this:
danh-sach-game/1-game-mang-xa-hoi (note: the number 1).
Then it works finds. Any paremeter index by a number will work find.
I rewrite url to a test file (replace index.php by test.php) than the page test.php receive the parameter as usuas, with or without number index the parameter.
This is because Joomla and most of the Joomla extensions uses ID column of the content as an identifier to look up the database. Some of the SEF tools (for example AceSef, sh404Sef) provide the facility to lookup using the alias name (the text after the number and hyphen) however with additional cost of database queries (they will in turn query for proper url internally).
The number in the last part of the URL will be processed and passed as ID of the particular page/content that you are viewing. This is done in the particular component's router.php file. So check the router.php file of the component you are using to check how the url gets parsed.

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]

mod_rewrite rule for anchors

I want to rewrite a url to a url with anchors
from "topic/14/599" to "topic.php?id=14#599"
this is my current rewriterule:
RewriteRule ^topic/([0-9]+)/([0-9]+)$ /topic.php?id=$1#$2 [NE,L,R]
this works fine, but the rewritten rule is in the adressbar !
so the browser's addressbar changes from "topic/14/599" to "topic.php?id=14#599"
I would prefer to keep the "simplified" version in the addressbar, the rewriterule however doesn't work without the L,R-flags, I just can't get it to work.
(if I just rewrite topic/14 to topic.php?id=14 without any flags it works just fine)
Update:
RewriteRule ^topic/([0-9]+)/([0-9]+)$ topic.php?id=$1&a=$2 [NE]
now works! the addressbar looks to the user like: mydomain.com/topic/14/599
and internally it is redirected to topic.php?id=14&a=599 and once the page is loaded, javascript jumps to the element with id=599!
Since anchors are processed on the client side, there's no way to rewrite to an anchor "silently" like you're trying to do -- the anchor has to be in the user-visible URL at some point. You could rewrite topic/14/244 to topic/14#244 as a redirect and rewrite topic/14 internally to topic.php?id=14, but you can't do both in a single step.

.htaccess not redirecting to ?_escaped_fragment_=

I want to redirect the request "index.php?_escaped_fragment_=about" to "snapshots/snap_about.html"
but I can't get it to work :(
It's an AJAX site; the URL I receive when I try to request example.com/index.php?_escaped_fragment_=about is:
example.com/index.php?_escaped_fragment_=about#!about
This is my htaccess
redirect index.php?_escaped_fragment_=about http://www.example.com/snapshots/snap_about.html
redirect /testx.html http://example.com/dummytest/index.html
The redirect /testx.html works.
Maybe I am making a big big mistake, but I am new at this!
I'd probably do this using a RewriteRule. Is this a one-off, or is it a general pattern that could be applied to other URLs? e.g. would you also want
index.php?_escaped_fragment_=blah to redirect to snapshots/snap_blah.html?
If not, something like this:
RewriteRule index\.php\?_escaped_fragment_=about snapshots/snap_about.html [NC,R=301]
If yes, something like:
RewriteRule index\.php\?_escaped_fragment_=(.*) snapshots/snap_$1.html [NC,R=301]

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