How to clean a url with htaccess? - php

I have a problem with url when loading PHP scripts.
The problem is that at the time of making the request to a php script, it loads normally, but when requesting another script, in the url they begin to gather and it looks like this:
www.example.com/file.php/route1/file2.php
I need this
www.example.com/file2.php
when i request another file, I need to have this
www.example.com/file2.php
What I need is to hide everything that it after file1.php or file2.php to load the other scripts without problems.

Without seeing your HTML content and .htaccess file it is hard to determine the exact cause of your issue(s).
Please verify the instructions in your .htaccess file. If you are using a Rewrite rule you need to validate that it is correct, for example:
RewriteRule ^/?$ "http\:\/\/example\.in" [R=301,L] If this rule is forcing a /route you obviously need to remove the /route from the instruction.
Please make sure to reference in your <a> tags the appropriate path. If you are not formatting it properly you will end up with concatenation. Are you using a framework? If so this may have an impact on your URL formatting.
Some example HTML a tags for you:
File 1 and File 1 will perform the same provided you do not have other factors impeding this simple approach.

Related

.htaccess redirect broken links to php page with parameters intact

I have a php website with a docs folder in the root, the structure of the site is thus:
sitename.com/docs/
In the docs folder are PDF's that sometimes cause browser issues because of spaces in the names, so they are not found (mainly by IE).
What I would like to do is the following:
Whenever there is a broken URL in the docs/ directory, I would like to redirect the entire query string to a php page within the docs directory, but it must keep the name of the PDF intact.
Thus, is the URL is:
website.com/docs/this is a pdf
I want it to redirect to:
website.com/docs/index.php?pdf=this is a pdf
From there, I can grab the PDF param and fix it up and send the request to the correct file.
The reason this is not done with straight .htaccess is that I cannot find a solution that is dynamic, in other words the number of words in the PDF is variable, and could be from 1 to 20 words, separated by spaces.
I had a post up here about that at this SO post which did get one reply, however, it still does not address the variable URL length problem.
I have again tried this from the examples in this tutorial but this has not helped me at all as I cannot fathom how to do this properly.
The one thing that I think is close is the following code:
RewriteEngine on
RewriteRule ^docs/(.*) /index.php?pdf=$1 [NC]
Am I close?
First you should know that, only static codes can be written in .htaccess, and we cannot process to a dynamic code,The Following solution might help for you am not testedRewriteEngine onRewriteRule ^docs/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?pdf=$1 [L]
Have your rule like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^docs/((?=[^\s]*\s).+?)/?$ /docs/index.php?pdf=$1 [NC,L,B,QSA]
This will forward all PDFs with spaces to /docs/index.php while leaving non space file names intact.

Using .htaccess file for good looking urls

What I'm trying to do is make my website's urls look prettier to the users.
For example I have this link in the index.php file in a href tags:
index.php?v=class&id=5
And I want the user to see in the address bar this:
www.mysite.com/class/5
I have tried using this code:
RewriteRule /([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?v=$1&id=$2
But I found out that it does the opposite. It makes the good looking urls turn into bad ones. It would take the www.mysite.com/class/5 and show this: www.mysite.com/index.php?v=class&id=5. What should I do?
You're almost there.
Don't use R in your RewriteRule. R is an external redirect (it tells the browser to redirect, so the URL changes). Instead, remove the R to do an internal redirect, that way Apache can still parse the query string normally, but the user sees the pretty URL (the URL doesn't change in the browser).
Have you tried these yet?
Here's some links I found when seraching Google:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.nouveller.com/quick-tips/quick-tip-6-how-to-write-clever-pretty-urls-with-htaccess/

PHP redirect to another website

I have a directory named "goto" and a file inside called index.php. Currently the following is inside the index.php file:
<?php
$url = $_GET['url'];
header("Location: $url");
?>
At the moment to redirect to another URL I have to type this into the address bar:
http://mysite.com/goto/?url=http://google.com
I would appreciate it if you could tell me how I could change that URL so that I could redirect the user to a website by typing this into the address bar:
http://mysite.com/goto/http://google.com
Use mod_rewrite and .htaccess to rewrite http://mysite.com/goto/http://google.com as http://mysite.com/goto/?url=http://google.com
RewriteEngine On
RewriteRule ^goto/(.+)$ /goto/?url=$1 [L]
Depending on your server configuration you may need to include a / in your rewrite path (i.e., ^/goto/(.+)$).
Unless you want to become a malware hub, I would wholeheartedly recommend you not doing this.
If you wish to allow redirect in such a manner, using http://mysite.com/goto/google and then work out the domain from a whitelist of available, allowed, destinations.
You will need to parse the data which could be a little tricky because you have to differentiate the difference between your URL and the other URL.
My suggestion is to not do so because the second that header is launched you will not see the url and it be better for you to just pass it as a get statement or a post.
EDIT
If you're determined then parse_url() is what you want. :)
#ide's method would work ... but you could also have the PHP script examine $_SERVER['PATH_INFO'], which is how that part of the URL would get passed to the CGI script.
(although, if there's a question mark in there, you'll also have to either make sure it's URI encoded, or also get the QUERY_STRING; you'll also lose any part after a hash, but you'd have the same problem with your current scheme)

how do i hide var passing info in the url if i have no form tags?

Hey guys - im using var passing links like this to jump around a site...
<a href = "index.php?content=about.html">
...problem is, i have all the ugly var info visible in the url. I would usually hide it by using the post method, but i dont have any form tags, so is it even possible?
Thanks!!!!!!!
It's a bad idea. GET is used for reading, POST is used for updating. A better solution would be to use some sort of mod_rewrite to make friendly URLS. Often called SEO friendly URLS...
Yes, you can POST with a <a href... but you have to have a lot of ugly javascript to do it... which breaks all sort of standard conventions.
Update, combining some new information
#FDisk has the simplest solution below, but I would add a condition to it which would allow existing files to be passed through directly by the webserver without having to run it through PHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
That way, a request to /images/bar.png will be served directly from the filesystem if that image exists.
Note, that your page does not necessarily need to have ".html" on the end anymore. So your URL could look like: http://example.com/about which would then be converted to: index.php?content=about
Taking it one step further from the link you listed in your post, you could then parse the url for various parameter. The example you looked up stuffed them into [$_GET, $HTTP_GET_VARS, $_REQUEST] respectively, but I think that's not such a good idea. Just make your own array of parameters.
You can try using the mod_rewrite extention
The original URL:
http://www.youwebsite.com/index.php?content=about.html
The rewritten URL:
http://www.youwebsite.com/about.html
.haccess file content:
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
you can hide info (var name and content) by encoding it. Thus the user won't be able to understand or change what you are passing around. But he will still see something in his url.
I guess you should give use some more context to understand why you cant use direct links to static pages ?

using seo user friendly in php

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).

Categories