URL rewrite problems - php

I wanted to pass the parameters to my PHP page in a friendly fashion, so here's what I wrote in .htaccess file
RewriteRule ^confirm\/(([a-z0-9]).+)\/(([a-z0-9]).+)$ www.example.com/confirm.php?email=$1&order=$2
its working well, and I can get the parameters in PHP using
explode('/', $_SERVER['REQUEST_URI'])
I have 2 problems though,
1- when I submit this page, I need it to go directly to confirmation.php, instead it submits to www.example.com/confirm/example#mail.com/12-HDYF/confirmaction.php !!! how can this be changed.
2- the page was originally working perfectly, but when I type the friendly URL all jquery files give me the below error, which really seems unrelated.

That is because the client (the browser) sees the page it is browsing as www.example.com/confirm/example#mail.com/12-HDYF/ and when it analyzes it and parses the html, it sees it something like:
<script src="jquery.js"></script>
<form action="confirmation.php"... >
So the browser sees relative urls to the page he is at the moment. So if you tell him to search the jquery.js file in the same folder as the one he currently is, he is going to try to download from www.example.com/confirm/example#mail.com/12-HDYF/jquery.js, and the same thing happens when you submit the form.
To avoid this, use absolute URLs (note the leading slash):
<script src="/jquery.js"></script>
<form action="/confirmation.php"... >
If you have your files in a subfolder of the DocumentRoot, put that subfolder at the beginning of the URL.

Change your url structure to avoid ^confirm for both raw and friendly URL, that should make things easier to debug (and avoid loops). This is a guess since you haven't posted your input that fails.
RewriteRule ^orderconf\/(([a-z0-9]).+)\/(([a-z0-9]).+)$ www.example.com/confirm.php?email=$1&order=$2

Related

Layout was broken in codeigniter and some jquery functionalities when changing http to https

I am a newbie with regard to https with codeigniter. I had a problem with my layout when I transfer to and https. In http everything works perfectly but when I purchase an ssl and use https in that matter some how it breaks my layout especially the homepage where I put some picture slider and images.
I would greatly appreciate any answers
I guess it creates some errors. You might wanna hide it in the index.php if there are notices, but you'd rather repair it if that's somethig else. Code for index.php
error_reporting(E_ALL ^ E_NOTICE);
You might be either not matching resources (e.g. if http and https directives aren't pointing to the same folder in your web server software configuration) or your pages are requesting non-https resources.
For the latter, try using relative paths to resources. e.g.:
<script src="scripts/somescript.js"></script>
or protocol-agnostic requests:
<script src="//example.com/scripts/somescript.js"></script>
Since you're using Codeigniter, set your base url in the config with https://
$config['base_url'] = 'https://mysite.com/';
Then when specifying urls to pages, images, etc within your domain, use the URL helper functions to automatically format your urls.
You want to avoid writing urls like this:
<script type="text/javascript" src="https://mysite.com/assets/jquery.js"></script>
About Us
And instead generate your urls like this:
<script type="text/javascript" src="<?php echo base_url('assets/jquery.js'); ?>"></script>
About Us
This way when you change anything like protocol, domain, etc you don't have to go through your application and update every single url by hand. The helper functions will take care of it.

Apache mod_rewrite syntax not working

OK guys, I have a problem that is literally driving me crazy. Here's what happened :
I decided that I wanted to rewrite the URL on my web-site.
It is supposed to rewrite from this syntax :
http://www.sample.com/programming.php?name=something
to this :
http://www.sample.com/tutorials/programming/something.php
Or (eg. 2) :
http://www.sample.com/other.php?name=test
to this :
http://www.sample.com/tutorials/other/test.php
So my URL syntax would be :
http://www.sample.com/tutorials/(name of my file)/(name of the variable).php
I have tried the following code :
RewriteEngine On
RewriteRule ^tutorials/programming/(.+)$ /programming.php?name=$1 [L]
RewriteRule ^tutorials/other/(.+)$ /other.php?name=$1 [L]
But, it doesn't rewrite the URL properly. The detailed explanation is below :
So, when I visit my original site, it appears like this :
http://www.sample.com/programming.php?name=something
If I visit this URL :
http://www.sample.com/tutorials/programming/something.php
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
http://www.something.com/index.php (Correct index.php URL)
but it's like this :
http://www.sample.com/tutorials/programming/index.php (which does not exist).
I have read over 10 tutorials online, asked my colleague to help me out, but neither did his solutions work. So, all I want to accomplish is that my URL is rewritten, so when the user choose a tutorial in programming, I don't get this URL in the address bar :
http://www.sample.com/programming.php?name=something
but this :
http://www.sample.com/tutorials/programming/something.php
and that is all I want.
I have tried to be as detailed as possible. If you need additional details, please, let me know.
Thanks in advance!
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
The relative/absolute paths you have in your page content is getting a different base because of the extra slash. When you have something like <img src="images/blah.gif">, the relative base is derived from the URL the browser sees (not what is internally re-written by the server). So if the browser sees: http://www.sample.com/programming.php?name=something, the URI is /programming.php and the URI base is /. But if the browser sees http://www.sample.com/tutorials/programming/something.php, the URI is /tutorials/programming/something.php and the URI base becomes /tutorials/programming/, which I'm assuming is not where your images/css/scripts/etc are located (since that directory probably doesn't even exist).
You need to either correct the URI base in all of your page headers by adding a:
<base href="/">
Or change all of your relative links to absolute links.
I get my web-site HTML, but without my CSS layout (just HTML displayed)
how did you include the css? did you include your files in the way
../../css/file.css
or in absolute mode
/css/file.css?
to check what really is the fault (i guess your rewrite success in your task eg i didn't understand it the right way) can you give us the real uri's?

how can i make script like this: <script src="mysite.com/dir/"></script>

i know, this looks stupid, but i am a new one in scripting so please help, i want my src not be ending with file with extension. like:
<script src="mysite.com/dir/"></script>
and NOT like this:
<script src="mysite.com/dir/script.js"></script>
is this done with HTACCESS or with what?
P.S. I want to create something like a hitcounter and dont want athers to see my script code
here is an sample:
<script type="text/javascript" src="http://topsite.ucoz.com/stats"></script>
if you run this in html it will sent a mini banner and an hiden iframe with that page.
Use url rewrites in your .htaccess like this:
RewriteEngine On
RewriteRule ^dir/$ /dir/script.js [NC]
The problem with this is that /dir/ cannot be used for anything else.
What you could do is something like this:
RewriteEngine On
RewriteRule ^script/$ /dir/script.js [NC]
However this does not solve the problem of people seeing your code at all.
The only solution to that is either code obfuscation or using php (or some other server-side language)
You can configure your web server to have a default page when you call a directory.
This is usually done for index.html, index.php, ...
And here say this default is script.js the server should then deliver it by default
The first one is to a directory and not a file. It doesn't really make sense unless your thinking how on most servers http://abc/index.html goes to http://abc/
Cant really do it and cant see why you'd want to.
Either use the htaccess code listed to rewrite your url or simply fill your index.php with javascript.
and actually, using a url-rewrite would make it pointless having an index.php/index.html there in the first place.
mysite.com/page.php:
<script type="text/javascript" src="/dir/"></script>
mysite.com/dir/index.php:
alert('hello world');
also the sample you listed shows an example of someone using a file, not a whole directory. they simply leave off the extension to their file. Extensions only have semantic value.
I think the main goal here is 'to hide javascript from the user' right?
If you want to hide a javascript, you may want to lookup for 'javascript obfuscation'.
You can't really hide javascript, because it must be able to run on the client browser.
Even if you set the default url, as answers stated above, the javascript file will be downloaded.
However, you can obfuscate javascript files so that it will be hard for humans to read while machines can execute it.
There is a question in SO that ask about it.
How can I obfuscate (protect) JavaScript?

.htaccess redirect subdirectory as query string in php

This is hard to explain, so hopefully I'm understood in my question.
(1) I want to create "SEO friendly" links that remove the query string from a web site. There is only one variable, let's call it "page". Here is the following code for my .htaccess file.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1
This works in providing the proper redirect. So /applications/ will send to index.php?page=applications.
(2) My index.php will include a view page based on the value of $_GET['page']. Here is some sample code below:
switch ($_REQUEST['page']) {
default:
include ("home.php");
break;
case "apps":
include ("apps.php");
break;
}
There seems to be no problems so far.
(3) Let's make apps.php an exact copy of home.php. home.php loads just fine, but apps.php will not load linked CSS and JScript pages. When apps.php is loaded, it thinks it is in the /apps/ directory. To load the linked pages, I would need to insert a "../" in front of the file name. Then it displays correctly.
So my question is -- How can I properly write the .htaccess file so the home.php and apps.php page can be identical files and produce identical results, instead of the apps.php file being treated as if it were in the /apps/ directory?
First, I should apologize as I don't have a solution which involves making changes in the htaccess. My solutions are of a different nature.
I think the problem can be solved if you have a config variable,preferably in a config file, which will hold the root folder for images, js etc. Most of the time its public_html, the document root, where the url of your website points to. so your config variable could look like:
$base_url = 'http://www.mywebsite.com/';
The config file should be included in index.php unconditionally.
So, when you include any js or images, you do it like this:
<script type="text/javascript" src="<?php echo $base_url;?>js/global.js" />
<img src="<?php echo $base_url;?>images/gradient_green.jpg" />
If you include the config file in index.php, all the files you include based on switch-case conditions, will be able to use the $base_url variable.
Another possible solution is to use the base tag. Look it up here:
http://www.w3schools.com/TAGS/tag_base.asp
I hope this helps.
use absolute urls for js, css and images on your pages (starting with a slash).
/js/main.js instead of js/main.js
You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). But then you expose the query string, which is what you wanted to avoid in the first place.
The reason it can't be done: It is not apps.php which "thinks it is in the /apps/ directory" - it's the browser which "thinks" that. In the page source generated by apps.php, you send relative URLs back to the browser, and now the browser will request these resources relative to the location of the page it asked for. For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side.
So the options you have are:
Do an external redirect with your .htaccess (and defeat your original purpose ;-)
Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs)
Use absolute URLs, just as #nobody has suggested in his answer.
The last one is the only real option IMHO.

php website url matching question

i am new to a php site, only familiar with .net web forms sites.
i can't figure out how routing is working on this php site.
www.oursite.com/suggestions.php is to suggestions.php
www.oursite.com/suggestions also loads the php fine
www.oursite.com/suggestions/ loads the php, but no css is applied
www.oursite.com/suggestions/anything - anything that comes after the '/' is ignored and suggestions is loaded without css. so oursite.com/suggestions////// works, as does oursite.com/suggestions/2/2/2/2/whatever
i have searched but not found any good explanation on how this is working. can someone explain or provide a good resource?
thank you.
This is most certainly done using Mod_Rewrite, an Apache extension. You'll probably find a file called .htaccess in the public root, in which these rewriting rules are defined.
DouweM has the right answer as far as the friendly urls are concerned.
As for the CSS, it is probably because you are using relative URLs in your link tags:
<link rel="stylesheet" href="site.css"/>
Change those to absolute URLs and it should solve that problem:
<link rel="stylesheet" href="/site.css"/>
The reason for this is that the browser makes the request for the CSS based on the directory it thinks it is in, even though your URL rewriting is changing that. So, if the url is http://mysite.com/suggestions/ and you are using relative urls, the browser will request the css as http://mysite.com/suggestions/site.css which of course doesn't exist.
www.oursite.com/suggestions.php is to suggestions.php
www.oursite.com/suggestions also loads the php fine
You probably have a .htaccess file that first checks whether or not a file of that name exists, and if it does serves it, then, if it doesn't, tries to route it to a php script.
www.oursite.com/suggestions/ loads the php, but no css is applied
The / means your browser considers '/suggestions/' a directory. If suggestions.php outputs HTML that contains a relative <link> to a stylesheet, e.g. <link href="style.css">, your browser will request www.oursite.com/suggestions/style.css, rather than www.oursite.com/style.css as in the previous two cases.
www.oursite.com/suggestions/anything
Same as the previous case, your browser will request the wrong css file, since it considers '/suggestions/' a directory. (For a potential fix, take a look at Eric Petroelje's answer.)
As DouweM said, though, your best bet is to look directly at your .htaccess file and figure out what it does.

Categories