Can PHP access the .htaccess rewrite name of a file - php

My .htaccess works (not an expert, but do a lot of copy & paste).
A typical rewrite for one of my pages looks like this:
RewriteRule ^my-grills-parts-and-service/?$ parts-service.php [NC,L]
...where the url "http://www.mydomain.com/my-grills-parts-and-service/" will invoke the parts-service.php file.
However, I am using php to construct a fully qualified URL, from what is in the address bar. The problem is, the server variable $_SERVER['SCRIPT_NAME'] will return "parts-service.php", rather than what is actually in the address bar, "my-grills-parts-and-service/".
I want php to somehow read the literal rewrite string, rather than the actual filename.
So, I want php to construct a string like this:
$url = "http://www.mydomain.com/my-grills-parts-and-service/";
I hope I don't have to write a data table or switch or if block, because that would be doing everything twice (once in the .htaccess, and again in a php file).

You can use $_SERVER['REQUEST_URI'] to get real url address.
Before using this method, check #regilero comment.

Related

How to clean a url with htaccess?

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.

PHP: How to get content of another URL (page) on my site without changing address in bar

I am using ModRewrite as below to convert urls on my site to be SEO friendly:
RewriteRule user/(.*)/$ seo-url-user-by-name.php?username=$1
Now I am writing code for seo-url-user-by-name.php and am looking for a way in PHP to redirect to:
user.php?uid=<uid>
so that seo-url-user-by-name.php will essentially return the contents of user.php?uid=<uid> BUT without changing the address in address bar to user.php?uid=<uid>
How do I do that?
Simply include user.php in seo-url-user-by-name.php. To get the querystring right you have to overwrite the value in $_GET.
$_GET['uid'] = 'whatever you want';
include 'user.php';
You're going about it backwards. The only URLs your code should be outputting are the 'friendly' ones. Those are the urls that will appear in the produced HTML and what will show up in the user's address bar.
e.g.
Bad URL (URL #1)
This URL is fine (URL #2)
You should never output anything but URL #2's. It'll be your server's responsibility to convert that clean (and in real terms, non-existent) URL to whatever really is on the server. PHP itself should never care nor see the /user/foo URL. PHP will be invoked as /user.php?id=foo as usual, and go about its business as usual.
The remote user would never see that rewriting occurring, they'll just see a request go out for /user/foo.

how to use $_GET with this url format site.com/extension/rar

I am seeing this url format at most websites.
site.com/extension/rar
I wonder how they get the value='rar' using $_GET.
What I know is that $_GET can be use in here
site.com/extension/index.php?ext=rar
Now I wanted to change my way of calling a variable.
I wanted to apply what most websites do.
How can I call variable in the former?
Perhaps this works to get the "rar":
$name = basename($_SERVER['REQUEST_URI']);
I most likely being done using .htaccess
It is an Apache module that allows you "rewrite" urls at the engine level based on your own set of rules. So basically it rewrites URLs on the fly.
So, in your example you could have a file named .htaccess with the following contents: (there may be other options)
RewriteEngine On
RewriteRule ^extension/([a-z0-9]+)$ somefile.php?extension=$1 [L]
Basically, you are saying: If someone is looking for a URL that looks like "extension/somenumbers-and-letters" then show the contents of "somefile.php?extension=whatever-those-number-and-leters-are".
Do a search on Apache mod_rewrite to find more information.

.htaccess redirection with query string to human readable url

I have a url: www.abc.com/change.php.
Parameters value is passed in post through $_POST[param].
I want to convert it to MVC like url through .htaccess such that:
www.abc.com/change.php with $_POST["height"] redirects to www.abc.com/change/height
and:
www.abc.com/change.php with $_POST["width"] redirects to www.abc.com/change/width.
How should my .htaccess file be changed for it?
I'm pretty sure you can't redirect or rewrite based on the name of a post variable.
Actually that's with .htaccess. If you redirect with php, you can definitely do that but that's not what you asked.
I guess you should also be able redirect/rewrite on get variable names.

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)

Categories