How to hide folders from URL using php or htaccess - php

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]

Related

Changing link apperance with .htaccess

I'm trying to do simple trick with .htaccess file, but with without success.
Generally I have PHP script that makes dynamically generated signatures and link looks like this:
example.com/signature/generate.php?name=%SomeUserName%
where %SomeUserName% is simply username e.g. Patison
and I'm trying to get:
example.com/signature/%SomeUserName% or (if necessary)
example.com/signature/generate/%SomeUserName%
Last code that I'm tried with no success:
RewriteEngine On
RewriteRule ^/signature/generate.php?name=(*) /signature/$1 [L,NC]
.htaccess is hard to understand for me.
So I have one more question. When someone use this link on another site he will render an image or it will work only on mine?
This is the sort of approach you need:
RewriteEngine on
RewriteRule ^signature/([^/\.]+)?$ signature/generate.php?name=$1 [L,NC,QSA]
So anybody going to signature/levi will see signature/generate.php?name=levi

Multiple vanity URLs for different php files?

I am trying to build a site which has the following pages which needs vanity urls,
account.php
questions.php
profile.php
I need to display
www.mysite.com/account?req=settings
www.mysite.com/account?req=questions
www.mysite.com/account?req=answers
as following
www.mysite.com/account/settings
www.mysite.com/account/questions
www.mysite.com/account/answers
And for the other files I need the following,
etc:-
www.mysite.com/questions?id=0484684
As
www.mysite.com/questions/0484684
And
www.mysite.com/profile?id=123456
as
www.mysite.com/profile/123456
Would anyone help me because I really need to make this possible.
I would like to remove the .php extention from the url too.
I like to say that I have no experience in htaccess files.
Thanks for the help, it worked !!!
In your .htaccess file, add these rules:
RewriteEngine on
RewriteBase /
RewriteRule ^account/([^/\.]+)/?$ account.php?req=$1 [L,NC,QSA]
RewriteRule ^questions/([^/\.]+)/?$ questions.php?id=$1 [L,NC,QSA]
RewriteRule ^profile/([^/\.]+)/?$ profile.php?id=$1 [L,NC,QSA]
You should be able to add new rules based on how these work if you need them in the future.
You'll want to use a .htaccess file combined with PHP for complex manipulations like that, I wrote an article on re-writing URLs after asking a similar question on SO:
http://tomsbigbox.com/elegant-url-rewriting/

Denying main links with .htaccess or anything else

I'm using an encrypted php program with smarty template.
I wanted to open links such as index.php?page=login to open with login.php, so I used Rewrite mode in .htaccesss, for example
RewriteRule ^/*login.php$ /index.php?page=login [L]
in a few pages there should be a query for errors such as
RewriteRule ^/*support.php?(.+)$ /index.php?page=support&%{QUERY_STRING} [L]
It works but I need to deny the main URLs so when users enters index.php?a=login manually nothing (or the main page) would show up and he should open the link only with /login.php.
Is there a way to do this?
A little help please...
It should work with a coirresponding rewirterule before(!) the other rules:
RewriteRule ^/*index.php?a=login$ / [L]

Pretty URLs from DB

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.

Friendly url rewriting with php & .htaccess. Difficulties with relative links to css images & js

I've currently an url like that : play.php?place=idplace&event=idevent
I've enabled friendly url this way :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ play.php?place=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ /play.php?place=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /play.php?place=$1&event=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /play.php?place=$1&event=$2
For just one parameter (the place id there), all is ok. But when it comes two params (place and event) like that mysite/theplace/theevent all my relatives links (css, js and imgs) are not working, my page seems to think it is actually in the theplace/ folder, and non in the root/ one.
Ideas for this issue?
Thanx a lot
Guillaume
You must use paths that are absolute from at least the domain, i.e.
/images/myimage.png
You may be able to fix this one problem in another way, but it will only become more annoying as your URL rewriting becomes more advanced.

Categories