I try to realize a system of rewriting URLs in .htaccess.
Then here is my goal:
If I have an url of this form: http://localhost/view.php?Id=456
Then I want to transform it to: http://localhost/456
I use this rule in htaccess:
RewriteRule ^ ([a-zA-Z0-9] +) $ view.php? Id = $ 1
Now this works very well!
But my problem I want to add points to id ie instead of 456 I can put: my.book
That is to say: http://localhost/my.book
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9\.]+)$ view.php?id=$1 [QSA,L]
You need RewriteCond %{REQUEST_FILENAME} !-f before the RewriteRule line to tell the server that the RewriteRule written below to be executed if the input passed in the URL is not an actual file. Because server searches for a file matching the input you pass in the URL and also it won't work in case you pass my.book in the URL since web server recognizes . as prefix for extension like .php or .html or like so and thereby it results in Not Found error if there is no file named my.book exists. So, you also need to escape . in the URL.
To allow .'s in the input, you need to add . with escape sequence \ in the character class group like ^([a-zA-Z0-9\.]+)$. Note, allowing this can result in escaping the extension in the URL, that is, passing view.php in the URL won't navigate to the actual file. Rather, it will be considered as a value in the query string.
Try this:
RewriteRule ^([a-zA-Z0-9\.]+)$ view.php?Id=$1
Basically what I did is I added \. with your pattern. This will make sure your regex matches any letter (small/caps), decimal numbers and periods (.). Hope this helps :)
Related
Note: The URL, code used here is only for demonstration purpose of my example.
I have seen that, For a HTTP GET request, if you want to pass a value for decision making, it is NOT passed through query String parameters for some good reasons.
Lets' say there's a thumbnail image showing a bookstore in houston location, say "ABC Bookstore"
The href attribute of that image is assumed as below
domain.com/texas/abcbookstore-houston
This is what is needed, and the page shows the book store details, instead of the URL being shown as domain.com/texas/details.php?id=1
Question:
Any ideas how the URL is analysed to fetch the key? One website when I looked at Network tab of Chrome, it showed
Request Headers
:authority:www.domain.com
:method:GET
:path:/texas/abcbookstore-houston
My thoughts:
I can extract the last word after parsing the complete URL, and I get 'abcbookstore-houston'
Code I tried:
$url = "domain.com/texas/abcbookstore-houston";
$path = parse_url($url, PHP_URL_PATH);
//echo $path;//// prints "/texas/abcbookstore-houston"
$parts = explode('/', rtrim($path, '/'));
$id_str = array_pop($parts);
echo $id_str; // prints abcbookstore-houston
My thinking is that we can have one more column in the main bookstore table called 'nameofid' and a query will fetch the 'id' whose 'nameofid' matches.
nameofid in this case is "abcbookstore-houston".
Summarized Question: Is this a correct approach? I have seen that in many of the websites, they no longer pass the query parameters even if that's a GET request, instead the URL looks clean like in this use-case.
As #charlietfl mentioned, I was actually looking at Clean URLs also called Pretty URLs
This is the actual book store details page
http://www.domain.com/texas/details.php?id=1
Basically this should be
http://www.domain.com/texas/details.php?id=abcbookstore-houston
I wanted this to be displayed in the address bar as
http://www.domain.com/texas/abcbookstore-houston
Finally, I found out the solution by making the below changes in .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:texas/)?([^/]+)/?$ details.php?id=$1 [NC,L,QSA]
The final command can be replaced by
RewriteRule ^texas/([^/]+)/?$ details.php?id=$1 [NC,L,QSA]
if 'texas' is mandatory and not optional.
So what is actually happening here is when I search for this URL http://www.domain.com/texas/abcbookstore-houston , the server is actually routing to http://www.domain.com/texas/details.php?id=abcbookstore-houston while we see only http://www.domain.com/texas/abcbookstore-houston in the address bar.
So inside details.php we can get the id using $_GET["id"] and continue our business logic.
Additional Notes:
If the objective was http://www.domain.com/abcbookstore-houston
then the RewriteRule would be
RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA]
Here's more explanation about the command used
RewriteEngine On turns the engine on.
RewriteCond %{REQUEST_FILENAME} !-f does not rewrite anything if the request filename exists, and is a file.
RewriteCond %{REQUEST_FILENAME} !-d does not rewrite anything if the request filename exists, and is a directory.
RewriteRule ^([^/]+)/?$ details.php?id=$1 [L,QSA] This is the actual rewrite rule. It takes anything after the domain name (anything other than forward slashes), and rewrites it to details.php, passing it as the id parameter.
RewriteRule ^texas/([^/]+)/?$ details.php?id=$1 [NC,L,QSA] This is the actual rewrite rule. It takes anything after {the domain name followed by the string 'texas'} (anything other than forward slashes), and rewrites it to details.php, passing it as the id parameter.
Note:
The technical term for word used in this use-case abcbookstore-houston is slug
https://en.wikipedia.org/wiki/Semantic_URL#Slug
A slug is the part of a URL which identifies a page using human-readable keywords.
To make the URL easier for users to type, special characters are often
removed or replaced as well. For instance, accented characters are
usually replaced by letters from the English alphabet; punctuation
marks are generally removed; and spaces (which have to be encoded as
%20 or +) are replaced by dashes (-) or underscores (_), which are
more aesthetically pleasing.
I have a url like this :
www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe
I am trying to conver this like below with the help of .htaccess file :
www.qwerty.in/1/abcd-cafe
I am trying the below but unfortunately its not working . can anyone help
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ details.php?vendor_id=$1&name=$2
You character must allow space as well:
RewriteRule ^([\w-]+)/([\w-\s]+)$ details.php?vendor_id=$1&name=$2 [L,QSA]
I have used \w, which is same as [a-zA-Z0-9_].
The following rule will convert your URL from www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe to www.qwerty.in/1/abcd%20cafe:
RewriteRule ^([^/]*)/([^/]*)\.html$ /details.php?vendor_id=$1&name=$2 [L]
It it much more generalized however, and will convert the first two parameter values even if they are not vendor_id or name. If this is not the behavior you'd prefer, have a look at anubhava's answer.
Converting a space to underscore can be achieved using:
RewriteRule ^(.*)\s(.*)$ $1_$2 [N]
Also, don't forget to ensure the rewrite engine is online:
RewriteEngine On
If you want to further experiment with these rules, I'd recommend a website I recently discovered: http://www.generateit.net/mod-rewrite/
I don't know if the title says it or not, but basically, I am using .htaccess to pass on an entire URL to my PHP file using a URL.
Example: http://example.com/var1/var2/http://example.net/logo.png/image.png
In this case, logo.png would be put inside image.png by my code.
I have tried Javascript to encode the variable URL.
http://example.org/utilities/banner-generator/Testyz/Testy/http%3A%2F%2Fstatic.example.com%2Ffiles%2Favatar%2F1498768_1.png/banner.png
This is what the URL looks like but Apache still treats the encoded slashes as normal slashes.
Is it possible to stop it from doing this?
Trick is to use B flag with THE_REQUEST in condition.
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+utilities/banner-generator/([^/]+)/([^/]+)/(.+?)/banner\.png [NC]
RewriteRule ^ /include/image/banner.php?name=%1&description=%2&icon=%3 [NE,B,L,QSA]
I am using wamp server for my projects and have stuck in making the urls clean
currently my url address is like this
http://localhost/BookProjectFinal/booksdetails.php?pid=8
I want to make it like this
http://localhost/BookProjectFinal/booksdetails/8
What I am using in my .htaccess file is this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ booksdetails.php?pid=$1
RewriteRule ^([a-zA-Z0-9]+)/$ booksdetails.php?pid=$1
But its not working its returning the whole url. What the problem with it? Thanks
The RewriteBase parameter will tell mod_rewrite from which directory to start matching. This can be used for applications that live in subfolders of apache. You may want to set it to /BookProjectFinal/ here.
Your regexp is looking to match a complete alphanumeric URL optionally followed by a slash, which I'm not sure is what you want. Try something like:
RewriteRule bookdetails/([0-9]+) bookdetails.php?pid=$1
to match only the number. Here's a test:
RewriteEngine On
RewriteRule ^bookdetails/([0-9]+) bookdetails.php?a=b&pid=$1
Matches URLs of the form /bookdetails/8. Use print_r($_GET) to sanity check in bookdetails.php
I have noticed that many websites use urls that end in
website.com/index.php?var="value"&var2="value2"
and I was wondering how I could make it so that instead of having that be the end of the URL have this instead:
website.com/value/value2
and then have it so that instead of searching for "/value/value2" inside of the servers root folder it would instead just open index.php and then inside the PHP coding have a function that would get what the URL is. Either as a string "/value/value2" or an array "value" "value2" it doesn't matter but just some way of getting those variables. This would be so that the URL could be cleaned up and easy to tell where you were in the website.
Also if there is a way of doing this would it be possible for style.php that is in the same folder as index.php (but has a PHP header setting it to output CSS) that would be called in the head of index.php using <link rel="stylesheet" type="text/css" url="style.php" /> or whatever the syntax for that is, to be able to obtain that same variable so that the css styling could be changed according to the URL.
You can use rewriting of urls in .htaccess file
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?var=$1&var2=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.
Refer Beginner's Guide to mod_rewrite.
Also tutorial for same.
You need to re write the URL.. if u are using apache you would have to add changes in the .htaccess file. Check this and this manual.
If using apache, enable mod_rewrite and use .htaccess
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
If using nginx, use nginx_rewrite_module http://nginx.org/ru/docs/http/ngx_http_rewrite_module.html
And inside your index.php parse $_SERVER['REQUEST_URI'] variable, it will contain requested url.
This can be achieved easily. Everything after the question mark are called $_GET variables. So you can call $_GET['var'] or $_GET['var2'] to get their values.
For example. I have the URL: http://www.example.com?username=username&password=password
Now i can take that url and make it so:
<?php
$user = $_GET['username'];
$pass = $_GET['password'];
$newUrl = 'http://www.example.com/' . $user . '/' . $pass;
echo 'Link text here';
?>
This results in a formatted url based on $_GET variables: http://www.example.com/username/password