I wanted to rewrite url in following pattern
My current url is:
xyz.com/news_details.php?id=120&title=hello-world
and I want the url as is:
xyz.com/hello-world
also I wanted to fetch the values of id on this page like:
$id = $_REQUEST['id'];
please help
I did use the following code:
RewriteCond %{THE_REQUEST} /news_details.php\?id=([^&\s]) [NC]
RewriteRule ^ /%2\.html? [NC,R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-])\.html$ /news_details.php?id=$2 [QSA,L,NC]
You could do something like this :
RewriteEngine On
RewriteRule ^/(.*)/(.*)$ /news_details.php?title=$1&id=$2
To achieve links like http://example.net/hello-world/120.
The ^/(.*)/(.*)$ part tells apache the pattern that it will follow. The second part (news_details.php?title=$1&id=$2) gives apache the real link that it will apply the pattern on.
You can't exactly hide arguments from the user, because you need a way to get the id that you want to have as the id argument. So that's the only possible way to do that.
You can read more about it here.
Related
I am trying to change my website URL according to get variables so that I can increase the security of my website.
For example I want to change my address, this is my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /category.php?cat_id=$1&mode=full&start=$1
And my website URL is:
http://joinexam.in/category.php?cat_id=17&mode=full&start=36
I want to convert this URL to:
http://joinexam.in/category/1736
where cat_id = 17
and start= 36
So it will become 1736 after the category.php page, I am trying to do it by using .htaccess file.
Here I want to take both cat_id and start as get variable, then according to these get variables, I want to change the URL of my website.
Can anyone explain the correct way to achieve this?
.htaccess
This forwards every URL to index.php.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
PHP
Get the "original" URL inside index.php:
// this gives you the full url
$request_uri = $_SERVER['REQUEST_URI'];
// split the path by '/'
$params = split("/", $request_uri);
Then you might route based on these $params.
As a good and fast router lib i suggest: https://github.com/nikic/FastRoute
By using this, you don't have to mess around with htaccess regexp stuff and can keep things at the PHP level :)
I have a website for listing education centers.I have two doubts.
I have a URL for printing one college details like this
www.example.com/education/eduS.php?Main=colleges&Name=nameOfCollege
I rwrite this url like this
www.example.com/education/colleges/nameOfCollege
my htaccess is
RewriteRule ^(.*)/(.*)$ eduS.php?Main=$1&Name=$2 [L,NC,QSA]
My problem is that
I want to skip the folder name eduaction from my url so as to get URL like this
www.example.com/colleges/nameOfCollege
How it is possible. I want to get Main and Name as GET parameters.
my Second problem is that , I have another url for listing colleges name in one page. The url like this
www.example.com/education/edu.php?Main=colleges&Category=Engineering-colleges
I rewrite like this
www.example.com/education/colleges/Engineering-colleges
my htaccess
RewriteRule ^(.)/(.)$ edu.php?Main=$1&Category=$2 [L,NC,QSA]
but this shows little bit confusion. The above two URL have same number of GET parameters. How can I handle this problem.
I also need to skip same folder name education from the second URL. Anyone help me
Use this code in your DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ education/eduS.php?Main=$1&Name=$2 [L,QSA]
This allows you to have your URLs like: /colleges/nameOfCollege
i store uploaded files at /storage/ this way
public-adam-luki-uploads-123783.jpg
park-hanna-adel-propic-uploads-787689.jpg
the '-' count unknown because it slice the pic description
i want my users to be able to access it as
http://site.com/public/adam/luki/uploads/123783.jpg
http://site.com/park/hanna/adel/propic/uploads/787689.jpg
i think it is the same problem here
mod_rewrite with an unknown number of variables
but i can't do it because i'm new to mod_rewrite module
i hope you can help me guys with the right rewriterule
The question you link to doesn't actually do what you are trying to do (although the principle is the same) what they do is convert the url to GET variables.
If all you want to do is convert / to - then you can use a simple rewrite rule that will run in a loop:
ReWriteRule ^(.*)/(.*)$ $1-$2 [L]
There are of course a few caveats to that...
Firstly, even if you are trying to get to a real directory/file the rule will still switch out / and - and leave you with a 404. You can get around that by adding conditions; to stop it rewriting real files:
RewriteCond %{REQUEST_FILENAME} !-f
You would do better however to limit the matches to only images (jpgs):
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^(.*)/(.*)\.jpg$ $1-$2.jpg [L]
Preferred Solution
ReWriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^images/(.*)/(.*)uploads[-/](\d+)\.jpg$ images/$1-$2uploads-$3.jpg [L]
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^images/(.*)$ storage/$1 [L]
This solution requires you to use urls like:
http://site.com/images/park/hanna/adel/propic/uploads/787689.jpg
The pseudo directory images means you can be sure that the url is actually one that you want to redirect and it doesn't break other images/links on your site.
The above rules take a url (like the example above) and transforms it like so:
images/park/hanna/adel/propic/uploads/787689.jpg <--- Original
images/park-hanna/adel/propic/uploads-787689.jpg
images/park-hanna-adel/propic/uploads-787689.jpg
images/park-hanna-adel-propic/uploads-787689.jpg
images/park-hanna-adel-propic-uploads-787689.jpg
storage/park-hanna-adel-propic-uploads-787689.jpg <--- Final
I was wondering if I'm supposed to use .htaccess to change a get variable to a value from the database (username).
so if there is
http://www.url.com/user.php?u=1
how do you conver it to
http://www.url.com/andrewliu
Thanks!
You need to change your user.php to take a username instead of a userid. Then you can use something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]
This passes the username through the u query string parameter, essentially: /user.php?u=andrewliu
Otherwise there's no way htaccess and mod_rewrite can know what the mapping is between user_id and username. Alternatively, you can write a database script and use RewriteMap to create a mapping for you:
RewriteMap usermap prg:/path/to/userscript
and in your htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=${usermap:$1} [L]
The last option, if you've only got, say, 5 users (or some small amount), you can make explicit rewrites:
RewriteEngine On
RewriteRule ^/?andrewliu$ /user.php?u=1 [L]
RewriteRule ^/?anotheruser$ /user.php?u=2 [L]
RewriteRule ^/?foousername$ /user.php?u=3 [L]
You need to use Mod-ReWrite. You can use this in your htaccess files but if you have access to your httpd.conf file it will prove to be quicker there.
You would ideally use the ?u=1 in your htaccess file and use that to find the the name and append the name onto the end. Otherwise you will have to search for the username and could be succesiptable to spelling mistakes etc etc.
RewriteRule ^/([0-9]+)/.*$ /user.php?u=$1 [L]
This is how Stackoverflow does it, try accessing this page without the number in the url and you will get a 404 page not found. Get the number right and what ever else you type after the forward slash will be exchanged for the correct words! This is much more convienent!
Use $GET in php to get the value of 'u'
Then use your logic to convert it to name.
Now just use redirect function to go to to that URL
I have a page (ie www.mysite.com/products.php?catid=5) in which I use the $_REQUEST[catid'] to get the category id and use it in a query. I switched to SEF urls and now the urls display like www.mysite.com/products/category/5
How can I use the new urls to retrieve the catid value?
The following lines were used in the .htaccess file for switching to SEF urls:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
You need to rewrite the URL correspondingly using mod_rewrite for example.
RewriteRule ^(.*)$ index.php?url=$1 [NC,QSA]
This rule would rewrite the URL www.mysite.com/products/category/5 to www.mysite.com/index.php?url=products/category/5. From this point on you can parse the variable $_GET["url"] or refine the rewrite rule.
Since the htaccess takes effect before the PHP starts to build you can grab the current URL with the below code snippet and get the value of the element as follows using the literal example of
www.mysite.com/products/category/5
$currentURL = $_SERVER['REQUEST_URI'];
$elements = explode("/",$currentURL);
$id_variable = $elements[3];
if you include the http:// in the url, then the element count should be 5 I believe
OR ... if you know that the id will always be the last element
$id_variable = $elements[(count($elements)-1)];
that will grab the last element in the array;
you can always use the following to show the data temporarily
echo "<div style='background-color:#FFF;'>";
echo ("Elements array:<br>\n");
print_r($elements);
echo "</div>";
hope this helps