Slim framework and .htaccess URL rewriting - .htaccess

I am creating a web API using Slim framework and now trying to rewrite the URL. I have wampp installed and have the following folder structure
E:\wamp\www\
.htaccess
E:\wamp\www\SlimTest\
index.php
.htaccess (not sure if I really need one at this level...)
Without any URL rewriting, I can access the Slim script by going to: http://localhost/SlimTest/index.php
Inside the index.php, I have a very simple Slim script:
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->write("Hello " . $args['name']);
});
And to access the /hello/name URI, without any rewrite, I type: http://localhost/SlimTest/index.php/hello/JoeDoe
What I want to achieve is to have the URL: http://localhost/slim/api/v1 URL redirecting everything to: http://localhost/SlimTest/index.php and everything after the index.php string is seen as the search query (so it reaches $app->get('/hello/{name}', properly)
Currently, the .htaccess at /www level has the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/slim/api/v1/(.*)
RewriteRule ^slim/api/v1/(.*)$ /SlimTest/index.php/$1 [QSD,L]
When I test the URL http://localhost/slim/api/v1/hello/John for example, the request is indeed taken to index.php. But, I've printed out the URI and what I got was:
echo 'current uri ' . $_SERVER["REQUEST_URI"];
"current uri /slim/api/v1/hello/John"
So, as you can see, the wholr /slim/api/v1/hello/John is taken as URI to the PHP script, while I want only everything after /v1/ to be taken to the script as URI...
Hope this makes sense. Can any one give me some direction, please?

Related

rewrite url must contain slash at the end of the url

I use url-rewrite to easy the process of finding a folder, but when someone enters something like this:
http ://domain.com/folder1/folder2
it returns a lot of errors.
Is there a chance of telling rewrite engine to automatically add slash at the end?
That meaning to change:
http ://domain.com/folder1/folder2
to:
http ://domain.com/folder1/folder2/
The file does not actually exist. I use ajax and History.pushState to change the url while ajax reads the folder.
And is there any GOOD online tutorial about rewriteEngine (not youtube)?
If /folder2 exists as directory in your filesystem ,then the following rule should add a traling slash to the uri :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ /$0/ [L,R]

How to rewrite url in htaccess without redirecting on localhost?

Trying to ask my question again and explain it the best I can.
I am running WAMP on windows 10. Apache version 2.4.18. My project is in www folder named hoidja.ee. mod_rewrite is enabled, AllowOverrideis set to All.
Inside that folder I have .htaccess file, where I am trying to accomplish rewrite without redirect and it does not work at all. I know the .htaccess file is working, since I can rewrite index.php to /home/ for example and it is working perfectly fine.
I have a page with all the users listing. You can click on the user, and it will take you to a url:
http://localhost/Hoidja.ee/hoidja.php?user_id=94&username=John
How I would like this to show is
http://localhost/Hoidja.ee/user/John
without any redirection.
What I have tried:
#tried first with only user_id
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule ^hoidja\.php$ /user/%1? [L]
This gave me 404 that /user/94 doesn't exist.
RewriteRule ^user/([0-9]+)/([A-Za-z0-9-]+)/?$ hoidja.php?user_id=$1&username=$2 [QSA]
This doesn't do anything visually. If I enter http://localhost/Hoidja.ee/user/94/John in the url manually, it goes to that page and shows the user data, but if I enter the original url it does not rewrite it. I am using <base> as well.
I have tried all the possible ways but nothing seems to work. How can I accomplish the url rewrite?
You are getting the 404 error because you are rewriting to a non-existent path /user/94 .you will need to redirect /hoidja.php?user_id= to /user/94 and then rewrite /user/94 back to the real location.
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule hoidja\.php$ /hoidja.ee/user/%1? [L,R]
RewriteRule ^(?:hoidja\.ee/)?user/(.+)$ /hoidja.ee/hoidja.php?user_id=$1 [L]

htaccess url'redirects and rewrites

Hope someone can help me out 'cause I utterly suck at .htaccess tweaking.
I'm developing a php based site. On this site I include specific pages based on a query string
eg: index.php?q=somePage.
In the .htaccess I look for eg: /somepage/ and tell the server to load q=somePage. When this is done the server strips all other query strings from the url ergo it does not pass them.
Now to the question, is there someway to setup the .htaccess to catch the [q] parameter and pass along all other querystrings (server side)?
eg: if I call /somepage&parameter=someparameter or /somepage/&parameter=someparameter the server will rewrite the url to /somepage/ but call the page (serverside) like so:
index.php?q=somePage&parameter=someparameter
Here's how my htaccess looks now:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?q=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?q=$1
if not I guess I'll just have to store the parameters in a session and pass 'em that way. But I hope someone can help :)
PS: If you know of a book that deals with the htaccess and can bring me from complete n00b to expert, it would be epic if you could point me in the right direction. the same actually goes for regEx ;)
Usual lot of framework move the logic of parsing the request inside the PHP
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
If the URL not identify a file or e dir, pass it to index.php
EDIT
You can see http://www.zytrax.com/tech/web/regex.htm for an introduction to regexp, but the suggest htaccess pass ALL the parameters to index PHP.
Use phpinfo(); to see how your reqest is seen by PHP
As your request here the index.php
<?php
phpinfo();
here some query you can try (I suppose your code on root of localhost):
http://localhost/
http://localhost/test
http://localhost/a/b
http://localhost/?p1=v1&p2&v2&p3&p4
http://localhost/a/b?p1=v1&p2&v2&p3&p4
http://localhost/a/b/?p1=v1&p2&v2&p3&p4

Redirect to fallback file if first attempt fails

I have this in my .htaccess:
RewriteRule ^images/([^/\.]+)/(.+)$ themes/current/images/$1/$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/([^/\.]+)/(.+)$ modules/$1/images/$2 [L,NC]
The idea is that it does the following:
// Rewrite this...
images/calendar/gear.png
// ... to this
themes/current/images/calendar/gear.png
// HOWEVER, if that rewritten path doesn't exist, rewrite the original URL to this:
modules/calendar/images/gear.png
The only things that change here are calendar and gear.png, the first of which could be any other single word and the latter the file name (possibly with path) to an image file.
I can rewrite the original URL to the first rewrite as shown in the example just fine, but what I cannot do is get my .htaccess to serve up the file from the other, fallback location if the first location 404s. I was under the impression that not using [L] in my first RewriteRule would rewrite the URL for RewriteCond.
The problem I'm having is that instead of serving the fallback file, the browser just shows a 404 to the first rewritten path (themes/current/calendar/gear.png), instead of falling back to modules/calendar/gear.png. What am I doing wrong?
Please note that my regex isn't perfect, but I can refine that later. Right now I'm concerning myself with the rewrite logic itself.
Fallthrough rules are fraught with bugs. My general recommendation is than any rule with a replacement string other than - should trigger an internal redirect to restart the .htaccess parse. This avoids the subrequest and URI_PATH bugs.
Next once you go to 404, again in my experience this is unrecoverable. I have a fragment which does something similar to what you are trying to do:
# For HTML cacheable blog URIs (a GET to a specific list, with no query params,
# guest user and the HTML cache file exists) then use it instead of executing PHP
RewriteCond %{HTTP_COOKIE} !blog_user
RewriteCond %{REQUEST_METHOD}%{QUERY_STRING} =GET [NC]
RewriteCond %{ENV:DOCUMENT_ROOT_REAL}/blog/html_cache/$1.html -f
RewriteRule ^(article-\d+|index|sitemap.xml|search-\w+|rss-[0-9a-z]*)$ \
blog/html_cache/$1.html [L,E=END:1]
Note that I do the conditional test in filesystem space and not URI (Location) space. So this would map in your case to
RewriteCond %{DOCUMENT_ROOT}/themes/current/images/$1/$2l -f
RewriteRule ^images/(.+?)/(.+)$ themes/current/images/$1/$2 [L]
Though do a phpinfo() to check to see if your hosting provider uses an alternative to DOCUMENT_ROOT if it is a shared hosting offering e.g an alternative environment variable as mine uses DOCUMENT_ROOT_REAL.
The second rule will be picked up on the second processing past after the internal redirect.

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

Resources