I'm having some trouble using rewrite engine to achieve my goal with friendly url.
Basically I have the following structure:
- index.php
- down
- index.php
My links in main index are something like this:
download/index.php?down=FILENAME
I would like it to rewrite something like
/download/FILE-NAME ( while pointing for the index.php inside download folder ).
I would like my links in first index could be used as well /download/FILE-NAME
My actual code:
RewriteEngine On
RewriteRule download/^([a-zA-Z0-9_-]+)$ index.php?down=$1
Can anyone help me achieving this? I'm constantly getting error due to I can't do it right.
After that, how can I get the the variable $1 after transform to seo friendly urls? I will have download/FILE-NAME and I need with PHP get the info from FILE-NAME.
So basically in the main index.php I would have a link like /download/FILE-NAME that will open the down/index.php file with the FILE-NAME ( staying something like.
Try this :
RewriteEngine On
RewriteRule ^download/([a-zA-Z0-9_-]+)$ /ifanydirectory/index.php?down=$1
And you can get variable using $_GET['down'] ifanydirectory is the directory name in which index.php exists relative path from root level, if not applicable simply use /index.php
do some experiments you'll get the correct one.
RewriteCond %{REQUEST_URI} ^download/(.*)$ [NC]
RewriteRule ^(.*)$ index.php?down=$1 [L,QSA]
to catch filename in index.php:
$filename = $_GET["down"];
Maybe I've not explain it well because it doesn't work as it's supposed.
I've edited the main question but try to explain better here.
I have a main index.php with links that would be
download/FILE-1
download/FILE-2
download/FILE-3
That links should open the folder down/index.php?down=FILE-NAME.
Isn't supposed that anyone can access "down" folder directly, so I would like to transform links in my main index.php like /down/index.php?down=FILE-NAME to something like /download/FILE-NAME.
Related
I have images and documents located after a series of folders like this:
http://domain.tld/library/data/info/history-of-america/hoa1.pdf
http://domain.tld/library/data/info/history-of-america/hoa2.pdf
http://domain.tld/library/data/info/50-moments-in-history/50mih.png
I have used php to redirect shorter links to these long URLs like so:
http://domain.tld/15
http://domain.tld/16
http://domain.tld/21
But because I am using the header redirect, when I click these short links they redirect to the files and display the long filepaths again.
How can I preserve the short links or at least get rid of the folders in the long links when the file is visible on the browser?
http://domain.tld/15
http://domain.tld/16
http://domain.tld/21
OR
http://domain.tld/hoa1.pdf
http://domain.tld/hoa2.pdf
http://domain.tld/50mih.png
Note I have hundreds of these files so manually inputting for each one is not scalable for me.
Thanks in advance. I've just been getting so much errors when trying other Q&A solutions so a clear path would be much appreciated.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^15/?$ library/data/info/history-of-america/hoa1.pdf [L,NC]
RewriteRule ^16/?$ library/data/info/history-of-america/hoa2.pdf [L,NC]
RewriteRule ^21/?$ library/data/info/50-moments-in-history/50mih.png [L,NC]
Include this in your .htaccess page
#url forwarded
RewriteRule ^([a-z0-9_.-]+)$ library/data/info/history-of-america/hoa1.pdf$1 [L,NC,QSA]
I have a URi Like this:
http://my.domain.com/clientarea/verify/
and I want it rewrites to this one:
http://my.domain.com/verify.php
I mean when the first URi called, it shows the result of the verify.php at root path.
Anyone knows how I can do it?!
Add this right after RewriteEngine on:
RewriteRule ^clientarea/verify/$ /verify.php
I am writing the following rules in htaccess file to change the query string as directory structure
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
is used to rewrite the url. It is working fine on
localhost/project/dashboard // (dashboard.php)
and all links are as
localhost/project/css/style.css
localhost/project/js/script.js
But When I append an id
localhost/project/dashboard/1 // (dashboard.php?user_id=1)
It changes all the links as
localhost/project/dashboard/css/style.css
localhost/project/dashboard/js/script.js
Why it is appending the dashboard to all links
How is the style.css referenced in your html file?
If you have it like this href="css/style.css", the HTML doesn't know you're rewriting, thinks /1 is a folder and will look in dashboard/css/style.css
Any system that uses url rewrite usually has to write all the path to the styles and scripts to avoid this. so you will have to reference your style like this
href="http://localhost/project/css/style.css"
if you have a production and development environment it will help you to have a variable like
if($SERVER['SERVER_NAME']=='localhost'){
$BASE_URL = "http://localhost/project/"
}else{
$BASE_URL = "http://mydomain.com/"
}
and put that before any call to css, scripts or images
;)
It's because you "tell him" to do that.
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
// ^here you tell him to print that "dashboard"
Of course other links works - you don't even match them with that rule.
I found you something here, scroll down to the title "Strip Query Strings". There, it say you to do this:
RewriteCond %{QUERY_STRING} example=
RewriteRule (.*) http://www.domain.com/$1? [R=301]
Just, of course, change that url to your own.
I am working on creating page links from DB like the following example.
Current page:
www.example.com/page.php?pid=7
In the DB it is saved as title "contact-us" under category "Company Info"
I want it to be like:
www.example.com/company-info/contact-us.html
I have tried different solution and answers but did not got any luck. I am not sure, where will be the PHP part and which rules to write for .htaccess files.
In apache (or .hataccess) do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /proxy.php?_url=$1 [QSA,L]
So in a nutshell, if the resource being requested doens't exist, redirect it to a proxy.php file. From there $_REQUEST['_url'] will be the url the user was requesting.
Then create proxy.php in your home directory and add whatever logic you'd like to load the correct content.
If you use this from .htaccess, then you may need to add RewriteBase / to your config.
If you want to find this page by url, you will probably do this through php and .htaccess. Make a .htaccess that calls page.php for each and every request. You don't need the pid=7, because, well, how should the .htaccess know it is 7, right? :)
In page.php, you take the original url and split it on the slashes, so you get the category (company-info) and the page itself (contact-us.html). Then, you can look these up in the database. This is in a nutshell how many software works, including Wikipedia (MediaWiki) and CodeIgnitor.
Mind that 'company-info' isn't the same as 'Company Info'. You'll have to specify the url-version in the database to be able to use it for look-up.
ok assume i have php page
has this name name.php?get= and has get varible named get
ok
how i can make it appear like that name?get=
If you are using apache, mod_rewrite is one way to go. There is a whole bunch of mod_rewrite tricks here.
I'd seriously reconsider before using (or overusing) mod_rewrite.
In almost all of my projects I use a simple mod rewrite in the .htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./
AddHandler php5-script .php
This tells the server to forward all pages to / (index.php) unless a file otherwise exists.
In the root directory I have a folder called "views" with all of the pages that I use. E.g. the file used for /home would actually be /views/home.php. However, in the index.php I have a script that parses the user's url, checks for the file, and includes that.
$page = substr($_SERVER['REQUEST_URI'], 1);
if(!$page) :
header("Location: /home");
if(file_exists("views/$page.php")) :
include "views/$page.php";
else :
include "views/$page.php";
endif;
This creates a variable called $page that stores the value of everything in the URL after the domain name. I use a substr() function on the Request URI to get rid of the trailing forward slash (/) on the URL.
If the variable is empty, for example if the user is simply at http://example.com or http://example.com/ then it forwards them to /home, where the script then checks for the home.php file inside of the views folder. If that file exists, it includes it, and displays it to the user.
Else, the script will simply include the 404 page telling the user that the file doesn't exist.
Hopefully this helps you, and if you need any further explanation I'd be happy to help!
I think you're wanting to re-write the URL client-side, which would include mod_rewrite.
In the route of your website, create a file called .htaccess and place the following code in it:
RewriteEngine on
RewriteRule ^name?get=(.*) /name.php?get=$1
Now when you type http://www.example.com/name?get=something, it will actually map to http://www.example.com/name.php?get=something transparently for you.
As far as i could understand your question, you can not strip the file extension because otherwise it will not run. In other words, you can not change:
name.php?get=
into
name?get=
But if you mean to create links with query string values that you can put them in hyperlinks in this way:
Click here !!
If you're looking to create links using a variable '$get', then you can create the link like this:
<a href="name.php?get=$get>Link</a>
Or if you want to get the value of the query string variable, you can use this:
$get = $_GET['get']