i trying to make 3 diffrent files diffrent url i use currently below code whis is work perfect for me.
RewriteRule ^([a-zA-Z0-9-/]+).html$ file.php?file=$1
and i feel i need more urls for 2 other files and i change this code as below but this not work it change url fine but it not redirect me to contact.php its redirect to file.php as above code work.
RewriteRule ^([a-zA-Z0-9-/]+).html$ contact.php?contact=$1
so i need to help for make 3 files diffrent urls.
How is apache supposed to tell the difference between a request that's supposed to go to file.php or a request that's supposed to go to contact.php? They look completely identical. You'll have to preface it with something unique, like:
RewriteRule ^file/([a-zA-Z0-9-/]+).html$ file.php?file=$1 [L]
RewriteRule ^contact/([a-zA-Z0-9-/]+).html$ contact.php?contact=$1 [L]
So the URLs will look like:
http://example.com/file/foo.html
http://example.com/comtact/bar.html
Related
I'm developing a web using PHP Codigniter,
I want to make my URLs look nicer,
For example I want this URL:
http://localhost/mlotfy/freelancing/quiz/view_quiz?id=25
To look like this:
http://localhost/mlotfy/freelancing/quiz/25
I've tried this Rewriting rule in .htaccess file but didn't work out(404 page not found)
RewriteRule quiz/([0-9]+) quiz/view_quiz?id=([0-9]+)
Try this
RewriteRule ^quiz/([0-9]+)/?$ quiz/view_quiz?id=$1 [NC,QSA,L]
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 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/
i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.
I am currently coding a pagination script into many parts of my site, this has been a well needed and requested feature and I have finally been able to come round and start coding it, it is all going well, until I find that my rewritten urls don't like working with the pagination urls.
So, an example page on my site would be news.php. This file structure can be something like news.php?id=5. I have rewritten the url like so:
/news/5/
## Rewrite URL's for News & Dev ##
RewriteRule ^news/([0-9]+)/$ /news.php?id=$1 [L]
RewriteRule ^news/([0-9]+)$ /news.php?id=$1 [L]
RewriteRule ^news$ /news.php [L]
RewriteRule ^news/$ /news.php [L]
The pagination script I am using prepends two new variables in the url, the new url turns out to be this:
news.php?page=1&ipp=55id=5
I would really appreciate it if anyone could assist me in making the urls look better, as it defeats the object of having it in the first place if after they use the pagination, it changes the url back to a clunky and ugly url.
I don't want it to be required to have parts of the url, that is something I really don't want..
e.g I don't want the url to be required to be /news/1/55/5, instead id like it to be optional.
Thank you for your time, it is appreciated!
Additional Information
The links in my news script currently display like so:
news.php?page=1&ipp=55id=5
I don't like to see ugly urls like that, and want to make the url look better using mod_rewrite, It would be better if the urls would display like so:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
Also, to make it as user friendly as possible, I don't want any of the fields to be required as such, so for example I would like to have the following link accessible at all times without it requiring the other fields.
/news/ID/
Then, when the user clicks a pagination link, it would use the following link structure:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
This is all from user feedback of my site, and is something that people have been asking for. Problem is, I don't understand even simple .htaccess
Thanks
RewriteBase /
# add slash to end of url if not present (and do a redirect)
RewriteCond $0 !/$
RewriteRule ^news([^\.]*)$ $0/ [L,R=302]
# rewrite url with format /news/[<id>/[<page>/[<ipp>/]]]
RewriteRule ^news/(?:([0-9]+)/)?(?:([0-9]+)/)?(?:([0-9]+)/)?$ /news.php?id=$1&page=$2&ipp=$3 [L]
Not sure what ipp is supposed to be, but my guess is it shows the number of item per page. I would personally not like to have that in my url.
You can have :
news/id/page/ipp with
RewriteRule ^news(/?)$ news.php [L]
RewriteRule ^news/([0-9]+)-(.*)_([0-9]+)(/?)$ news.php?page=$1&ipp=$2&id=$3 [L]
news/1222-subjet-for-example_34
return :
news.php?page=1222&ipp=subject-for-example&id=34
use (/?) instead of create many rules ;)
Hope it's works for you.