htaccess below diverts PDF calls to validate.php where name of the PDF is get with $_GET['filename']
Problem is, it works with URLs below but it doesn't when there are many sub directories so what I need is, instead of just getting name of the file, I should pass whole URL to validation.php something like ^(.*)$ /validate.php?request_url=$1 [L] (this is just an example).
Note: instead of whole URL, anything after domain is acceptable.
WORKS:
http://www.whatever.com/1.pdf
http://www.whatever.com/2.pdf
http://www.whatever.com/3.pdf
WON'T WORK:
http://www.whatever.com/sub1/1.pdf
http://www.whatever.com/sub1/sub2/1.pdf
http://www.whatever.com/sub1/sub2/sub3/1.pdf
How can I do it?
Thanks
CURRENT HTACCESS:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^(.*)$ /validate.php?filename=$1 [L]
CURRENT VALIDATION.PHP
if (isset($_GET['filename']))
{
//Do something with it
}
else
{
//Don't do anything
}
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^ /validate.php?filename=%{REQUEST_URI} [L]
Try this:
RewriteRule ^(.*)\.pdf$ /validate.php?filename=$1 [L]
without the RewriteCond
Related
This is my .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^(.*) data.php?chn=$1 [L,QSA]
I have 2 php files.
index.php and data.php
I to want call index.php when the request is for domain.com/ and data.php when the request is for domain.com/(anything) .
To reroute from index.php...
if (!empty($_GET['chn'])) {
require('data.php');
exit();
}
data.php will already have $_GET['chn'] available to it.
or if you aren't queuing off a get var then you can check the REQUEST_URI and do the same thing:
if (#$_SERVER['REQUEST_URI']!='/')) {
require('data.php');
exit();
}
RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/(\d+)*$ ./data.php?chn=$1
Try this.
Note: When using .htaccess in your desired case, placing / after the domain name will trigger the second RewriteRule case, meaning user will try to open literally domain.com/data.php?chn=.
You need to change your regex pattern from (.*) to (.+) to avoid rewriting your homepage otherwise the request for / will also be rewritten to data.php :
Try this rule :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/data\.php [NC]
RewriteRule ^(.+) data.php?chn=$1 [L,QSA]
its work ! thank you all using this code :
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ data.php?chn=$1 [L,QSA]
I have an index.php file, with the following lines in it:
if(isset($_GET["page"]))
{
$page=$_GET["page"];
setcookie("page",$page,time()+315360000);
}
else if(isset($_COOKIE["page"]))
{
$page=$_COOKIE["page"];
}
else
{
$page="stock";
}
It's a simple page handler script, if you don't have the variables in your URL, the last viewed page will be opened.
Now I want to rewrite the URLs with a .htaccess file, and I have really strange results.
My webpage was hosted on a server like www.host.com/directory/index.php
Basically, I want the URLs look like www.host.com/directory/$page
The .htacces file was placed to the "directory", and have the following lines in it:
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1 [NC]
My problem:
If the system can find a file based on the $page variable, it will be included. If the file doesn't exists, an error page will be displayed. Now, on the error page I echoed out the $page variable, and it is "index.php".
What is wrong with my code, and how can I fix it? I've never written any .htaccess before...
Thanks for your answers!
Your rule is looping, you need to add some conditions to prevent this from happening:
RewriteEngine on
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?page=$1 [NC]
or
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [NC]
I'm trying to rewrite some url's from a $_GET request with .htaccess with no luck.
How's the syntax for (left side = original url, right side = new url)
/?lang=en ---> /en/
/index?lang=en ----> /en/index
/pageA?lang=de ----> /de/pageA
/pageB/lang=es ----> /es/pageB
Can this be done without editing manually all the pages and get requests in the .htaccess file?
Thanks in advance for helping.
EDIT
This is my code so far to eliminate the .php at the end in the URL
Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
It's only the code which is working.
Something like this could work:
RewriteRule ^([a-z]+)/([a-zA-Z]+)/$ /$2?lang=$1 [QSA]
This is what I came up with:
RewriteRule ^(([-A-Za-z0-9_]+)?(\?|\/)lang=([A-Za-z]{2}))$ redirect.php?page=$1 [L]
EDIT:
If you want to redirect to the clean URL, then what you probably want to do is make one central php page to go to. From that page, determine where you want to go and then do a header("Location: ".$real_page); exit;
Here's an example:
<?php
// REDIRECT.PHP
$page = $_GET['page'];
$real_page = preg_replace('/([-A-Z0-9_]+)?(\?|\/)lang=([A-Z]{2})/i', '/$3/$1', $page);
header("Location: ".$real_page);
exit;
EDIT 2:
If you want to keep it all withing .htaccess and not mess with the PHP redirect, you can use the [R=302] flag.
RewriteRule ^([-A-Za-z0-9_]+)?(\?|\/)lang=([A-Za-z]{2})$ /$3/$1 [R=302]
That should actually change the URL for you.
Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url
I have this .htaccess that I've been using to rewrite URLs like these:
www.example.com/index.php?page=brand www.example.com/brand
www.example.com/index.php?page=contact www.example.com/contact
www.example.com/index.php?page=giveaways www.example.com/giveaways
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
I used a file called index.php to handle the redirects. Code used below:
$page = trim($_GET['page']);
if($page == "giveaways")
require('pages/giveaways.php');
Now, I would like to add another URL type like these:
www.example.com/index.php?page=products&p=ford-mustang
TO
www.example.com/products/ford-mustang
How will I accomplish this? Any suggestion would be greatly appreciated.
You need to add a second RewriteRule above your current one.
Here's an example:
RewriteRule ^/(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA]
This will rewrite /product/ford-mustang to index.php?page=product&p=ford-mustang
Remember to add it above your current RewriteRule, because it first tries to match the first RewriteRule, when there's no match it will go on with the second RewriteRule and so further.
A URL rewrite for /products/ford-mustang would be:
RewriteRule ^(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA]