Hi Everyone, I am looking for some help with a htaccess rewrite rule.

At the moment I have

RewriteRule ^(.+)$ /profile.php?slug=$1 [QSA,L]

As one rewrite rule in my htaccess file, this works as expected and rewrites my url to domain/user-name
(profile.php is in my root domain)

What I am trying to do now is rewrite
domain/dir/detail.php?title=junior-real-estate-personnel-required&ref=29681097
To
domain/dir/junior-real-estate-personnel-required

How can I get the url to look like above whilst still bringing back the correct information from the backend database.
Is it possible?

If all my links on the website point to domain/dir/title
How will the database know what information to display if I have two identical titles?

Member Avatar for diafol

You can always use a session variable to keep "context", BUT this is not good if the "title" page is a new landing page for example as there will be no "context" in sessions vars. In addition, having a common name for multiple pages may play havoc with SEO.

EDIT

Forget what I just said - I noticed that "dir" means "user-name"

I'm assuming therefore that...

domain/diafol/something-good/

would refer to domain/detail.php?slug=diafol&item=something-good

You lose the id (ref) unless you do as DW I think does...

domain/diafol/29681097/something-good/

would refer to domain/detail.php?slug=diafol&item=something-good&ref=29681097

Hi thanks for your reply.

I think I have confused you (sorry)
RewriteRule ^(.+)$ /profile.php?slug=$1 [QSA,L] relates to
domain/profile.php?slug=user-name
Rewrites to domain/user-name

What I am trying to do now is rewrite
domain/dir/detail.php?title=junior-real-estate-personnel-required&ref=29681097
To
domain/dir/junior-real-estate-personnel-required

If this would cuase issues for SEO, due to duplicate urls
I'd be happy to rewrite the url to
domain/dir/29681097/junior-real-estate-personnel-required

Here is what I have at the moment -

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /profile.php?slug=$1 [QSA,L]
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)\$ detail.php?title=$2&ref=$3

My attempt to rewrite
domain/jobs/detail.php?title=junior-real-estate-personnel-required&ref=29681097

To:

domain/jobs/29681097/junior-real-estate-personnel-required

Member Avatar for diafol

Ah OK, now I gotcha. Yep, I think you have to have the ref in rewritten url otherwise duplicate titles can't be resolved.

It's either that or you need to put a field in the table called something like "position" or "version" or whatever, so that when duplicate titles are stored, they get an increment. The rewritten url then:

domain/jobs/detail.php?title=junior-real-estate-personnel-required&v=2

domain/jobs/junior-real-estate-personnel-required/2

The DB then gets the item that matches the title and the version number. If version number doesn't exist, then default to 1. I don't think this is an improvement and if anything will cause a slower query. Maybe even pointless...

The reason I have added the ref into the url is due to the possibilitiy of duplicate job titles.

Is this the correct way to rewrite the url
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)\$ detail.php?title=$2&ref=$3
domain/dir/29681097/junior-real-estate-personnel-required

Keep it simple. Keep the ID in the URL like we do ;)

You can subsequently offer members a "vanity URL" where they can choose a unique username for the URL that links to their profile which doesn't include an ID.

If you want people to ever manually type in the URL (Such as "go to my twitter feed at twitter.com/daniweb!") then it's important to have a vanity URL that doesn't include an ID #. But if pretty much 100% of the traffic to the page will be from an end-user clicking on some other link somewhere on the web to end up there, then keep it simple, keep an ID # in the URL, and call it a day.

Hi Dani, thanks for replying. We were chatting the other day on Twitter
"About doing an all nighter"

User already have a "vanity" url created when they register using their first name and surname. ie. domain/user-name - Users can type their url and be presented with their information, website visitors can view other member profiles by clicking a link.

The url I am trying to rewrite is for a different part of the website.
The domain/jobs/details.php?title=junior-real-estate-personnel-required&ref=29681097

The reason I have added the ref into the url is due to the possibilty of duplicate job titles.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.