I'm trying to write a rewrite rule in my .htaccess file that has the following behavior. I'm doing it for an API which is located at a real location in the form of:
https://www.domain.com/apifolder/entry.php?call=testcall&data=testdata
The API call is routed starting from entry.php and eventually delivers the appropriate response. Essentially my entire API lands upon a large switch statement of valid API calls. The problem is, that URL is not very nice. I'm trying to replace it with a URL of this form:
https://www.domain.com/api/testcall?data=testdata
I have gotten close with the following rewrite rule:
RewriteRule ^api/([^/]+)/? /apifolder/entry.php?call=$1 [L]
However, the desired example above does not work, because there first parameter that seems to coming in (data, which is in reality the second parameter), is led by a ?. If I try the url:
https://www.domain.com/api/testcall&data=testdata
where it's led by a & instead, then it works perfectly fine. This looks weird to me too, sadly. Is there anything I can do to let the rewritten URL take in the query string beginning with a ? instead?
I managed to solve this by including the [QSA] flag in the rule, which passes the query string (in its desirable format) to the real URL:
RewriteRule ^api/([^/]+)/? /apifolder/entry.php?call=$1 [QSA,L]
I tried it after seeing this StackOverflow answer: Match Question Mark in mod_rewrite rule regex
Related
I need help with using RewriteCond properly in my .htaccess.
I have a simple php file which accepts 2 get parameters and returns a JSON object. Well, I wrote a simple Rewrite Rule to make my url pretty and understandable.
Although after adding a form, obviously the Rewrite Rule won't get triggered because it will send a standard 'raw' URL.
My normal URL looks like this:
www.example.com/?ost=1&song=2
After my Rewrite Rule:
www.example.com/ost/1/song/2
But the form sends the url like this: www.ost.com/?ost=1&song=2
After searching a bit, I found out that RewriteCond could help but I am not familiar with it.
Edit:
I have been asked for my Rule, so here it is:
RewriteRule ^(ost)/([^/.]+)/?(song)?/?([^/.])?$ index.php?$1=$2&$3=$4
I forgot to mention that the song parameter is optional but this doesn't matter.
Hello guys. I started coding my own "URL shortener". The basic idea is you use example.com/12345 to redirect to another URL. This "matching" is done by using .htaccess to redirect stuff towards a script that does (irrelevant for us now) stuff.
My .htaccess currently looks like this:
RedirectMatch 302 ^/\w{5}$ /redir.php
The redirect matches any string of exactly 5 and sends it toward my PHP script where the actual redirection to the expanded URL take place. The only problem is that I was unable to find a proper way of getting the original URL, the matched one into a variable.
As a sidenote the whole thing happens on a VPS set up by me with minimal knowledge, so if this problem can originate from a missing config ($_SERVER['HTTP_REFERER'] doesn't work), then expect my configs to not be 100% correct and by standards.
EDIT: changed from RedirectMatch to RewriteRule, still doesn't work.
RewriteRule ^\w{5}$ /redir.php [R,L]
you can use the following rule:
RewriteRule ^(\w{5})$ /redir.php?redir=$1 [R,L]
this will send the 5 letter string as querystring param redir. Which you can access in redir.php as:
$_GET['redir']
Edit: Or as #LawrenceCherone have suggested you can use $_SERVER['REQUEST_URI'] in redir.php. But for that you have to use NC flag in .htaccess instead, Like:
RewriteRule ^(\w{5})$ /redir.php [NC,L]
I am trying to use mod_rewrite to redirect users keeping the multiple query string and creating a redirect page
For Example,
If user opens
http://localhost/url/url/http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
then he is taken to
http://localhost/url/url.php?redirect=http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
There is secondary problem that URL should be encoded and then redirected! If URL is not encoded then the string (&stackoverflow=great)would be not a part of 'redirect' string of url.php
I tried many solutions then came for stackoverflow! I tried the following code in following file
http://localhost/url/.htaccess
RewriteRule ^url/([^/])$ url.php?redirect=$1 [QSA,L]
but the result is localhost/url/url.php?redirect=http only
Your setup won't work with the unencoded inner url, so an 'answer' can only have temporary character. But this might be a starting point:
RewriteEngine on
RewriteRule ^/url/url/(.*)$ /url/url.php?redirect=$1 [L,QSA]
I wonder however if that fragment /url/url is really intended (the two 'url's in there).
Note that the exact rule content also depends on where you want to define that rule. The syntax is different whether you use the central server configuration (referred) or .htaccess style files (as second choice and more complex).
Try this
RewriteEngine on
Redirect ^url/url/(.*)$ url/url.php?redirect=$1
The basic redirect systax,
redirect accessed-file URL-to-go-to
I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?
this is the URL path I currently use:
/index.php?page=1&title=articles
I want to get the URL path as
/index/page-1/title-articles
using SEO user friendly URLs in PHP.
And how to get the value of the "title"? Any one can help me pls.
Check out the mod_rewrite module, and maybe for a good starting point, this tutorial on how to take advantage of it with PHP.
You need to ensure two things:
your application prints out the new URLs properly, and
your webserver can understand that new URLs and rewrites them to your internal scheme or redirects them back to your application and your application does the rest.
The first part can be simply accomplished by using
echo ' … ';
instead of
echo ' … ';
The second part can be accomplished either with URl mapping features of your webserver (most webservers have a module like Apache’s mod_rewrite). With mod_rewrite, the following will do the rewrite:
RewriteEngine on
RewriteRule ^index/([^/-]+)-([^/]+)(.*) /index$3?$1=$2 [N,QSA]
RewriteRule ^index$ index.php [L]
The first rule will extract one parameter at a time and append it to the query. The second rule will finally rewrite the remaining /index URL path to /index.php.
I want to get the URL path as
/index/page-1/title-articles
Why? I’ve got two objections:
index is a no-information and I doubt that it belongs in the URI
page-1, as well as title-articles looks plain weird. Like with index, you should ask yourself whether this information belongs here. If it does, make clear that it’s the key of a key-value pair. Or remove it entirely.
Thus, I propose either:
/‹article›/1
or
/‹article›/page/1
or
/‹article›/page=1
or
/‹article›[1]
or
/articles/‹article›/page/1
Or any combination thereof. (In the above, ‹article› is a placeholder for the real title, the other words are literals).