Custom url rewrite help needed - php

I have created a database with url slug and it contains urls like mysite.com/blog/content/user1 and blog is folder where file is so I want to change mysite.com/blog/page.php?name=content&user=user1 to mysite.com/blog/content/user1 how can I do that I have tried
RewriteEngine on
RewriteRule ^([a-z]+)$ page.php?name=$1&user=$2 but that doesn't works

Try this one
RewriteEngine on
RewriteBase /
RewriteRule ^blog/([^//]+)/([^//]+)$ blog/page.php?name=$1&user=$2 [QSA,L]
Please avoid making the slug record using / which means the uri segment better to use - for you slug so if your url is something mysite.com/blog/page.php?name=content&user=user-a and you want to show like mysite.com/blog/content/user-a them above rule will also work for that url aso

Related

Rewrite urls php/mysql page with htaccess apache

I have a search form to search for a health institution in a city.
when I display the results I have this URL for example :
search.php?city=mycity&speciality=cardiology
I would like to rewrite this URL like this: health-institution-cardiology-mycity
I set up an .htaccess rule like this :
RewriteEngine on
RewriteRule health-entity-([a-zA-Z\-]+)-([a-zA-Z\-]+) search.php?city=$2&speciality=$1
It doesn't work, however, I applied this rule to another type of URL and it works
is there any issue with the code?
do I need to add something?
This will work:
RewriteEngine on
RewriteRule ^health-institution-([a-z]+)-([a-z]+)/?$ search.php?city=$2&speciality=$1 [NC,L]
Note: The NC (No Case) at the end makes the rurl case insensitive.

Change links type from .php?id= to html

I have a file hosting and sharing PHP script, which uses the basic type of links, when a file has been uploaded, the basic link of the file will be :
http://example.com/do.php?id=1
Noting that the (1) is the file ID.
Now the script has a feature to enable the mod rewrite with HTML links type.. when Activate it .. the link above will be like this:
http://example.com/download1.html
Now, the htaccess file need to be edited to change the links to another shape.. So what I want to do is to make the above link looks like:
http://example.com/filename_FileID.html
or
http://example.com/FileID.html
or
http://example.com/filename_RandomCharectars&numbers.html
and I don't know how to do it .. Please help me editing my htaccess..
Here is the currect htaccess file:
RewriteRule ^download([0-9]*).html$ do.php?id=$1
RewriteRule ^downloadf-(.*)-([a-zA-Z0-9_-]*).html$ do.php?filename=$1&x=$2
RewriteRule ^down-([0-9]*).html$ do.php?down=$1
RewriteRule ^downf-(.*)-([a-zA-Z0-9_-]*).html$ do.php?downf=$1&x=$2
RewriteRule ^downex-([0-9]*).html$ do.php?down=$1
RewriteRule ^downexf-(.*)-([a-zA-Z0-9_-]*).html$ do.php?downexf=$1&x=$2
For http://example.com/FileID.html (I'm assuming "FileID" is going to be a numeric value)
The rule is
RewriteRule ^([0-9]*)\.html$ do.php?id=$1
For http://example.com/filename_FileID.html
The rule is
RewriteRule ^filename_([0-9]*)\.html$ do.php?id=$1
For http://example.com/filename_RandomCharectars&numbers.html
The rule is
RewriteRule ^filename_(.*)\.html$ do.php?id=$1

URL Rewrite is not working properly

This is my URL rewrite:
RewriteRule ^cars/$ cars.php
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1
This works, so my URL is like this:
example.com/cars/porche/
refers to
cars.php?model=porche
Now Im making more search criterias, so I want to be able to add for example model year, car manufactor, etc. like this:
example.com/cars/porche/?model_year=xxx&car_manufactor=xxx
Right now this does not work with the current rewrite, but I can't figure out why.
Simple, just add QSA:
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1 [QSA]
That tells the rewrite engine to append any other query parameters to the rewritten URL as well.

.htaccess with pagination in php?

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.

.htaccess rewrite friendly URLs

I am not sure if this is possible, but I have the following URL that I want to make more friendly:
http://www.myurl.com/content.php?id=13089093057550&slug=this-is-a-nice-url
And I want it to look like this:
http://www.myurl.com/this-is-a-nice-url
How do I do this?
You'll need to pass the id to your PHP code in order to display content I believe. For example, look at the Stack Overflow URL for this question: http://stackoverflow.com/questions/6520671/htaccess-rewrite-friendly-urls where it is passing both id and slug. So you can have friendly URLs like:
http://www.myurl.com/13089093057550/this-is-a-nice-url
If you decide to go by my suggestion then here is the code you will need in .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^([0-9]*)/(.*)/?$ /content.php?id=$1&slug=$2 [L,QSA]
You can get the slug into the querystring, but without providing the id somewhere it's impossible for Apache to supply it. Hopefully you have a way to get the id out of your database using only the URL slug parameter.
RewriteEngine On
# We don't know the id
RewriteRule (.*)$ /content.php?slug=$1 [L,QSA]

Categories