Hi all.
I have a site with articles and members and since the url ends for both like.. site.com/articles.php?id=232 or site.com/ref.php?user=3232
I costumised this script I found online to costumize the url. I did for the profiles which is working, but I can't add for the articles:

RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]

that code is modified to redirect user profiles which would look like site.com/ref.php/user=47 to look like site.com/john

But now I need the same for news article urls too. In my page, the url for articles is like site.com/articles.php?id=1232 and I need it to be like site.com/articles/here_is_todays_article

So, my question is how do I add to the above code?

Recommended Answers

All 11 Replies

If your dynamic url is:
site.com/articles.php?id=1232

I believe this should be correct;

The whole string would be:

RewriteRule ^articles/([A-Za-z0-9-]+)$ articles.php?id=$1 [L]
RewriteRule ^articles/([A-Za-z0-9-]+)/$ articles.php?id=$1 [L]

This part of the url is handled and translated in PHP.

site.com/articles/here_is_todays_article

Thanks. I was waiting for a reply really bad.
But, I copied your code and pasted it and this is what the .htaccess looked like

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]
    RewriteRule ^articles/([A-Za-z0-9-]+)$ articles.php?a_id=$1 [L]
    RewriteRule ^articles/([A-Za-z0-9-]+)/$ articles.php?a_id=$1 [L]

Sorry btw its (...?a_id=12312) NOT (...?id=1232)

But, When I clicked articles it took me to site.com/articles.php?a_id=1231
So, it did not make any difference at all.

If you clicked the link and it took you to a dynamic url such as: site.com/articles.php?a_id=1231

This means you need to make PHP translate your dynamic links.

Test this by typing in the url address bar: site.com/articles/1231

Or by typing: site.com/articles/article_title_by_id

Also try putting the string above; RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^articles/([A-Za-z0-9-]+)$ articles.php?a_id=$1 [L]
RewriteRule ^articles/([A-Za-z0-9-]+)/$ articles.php?a_id=$1 [L]
RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]

It's strange. It does not work.
For the user, the url used to be site.com/ref.php?user=12 so, after making this .htacess

RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]

If I click on the username it takes me to site.com/username So it Works
but, nothing with for the article. Are you sure you are not forgeting anything?
thanks btw.. :)

Also in some cases you may need to define the; RewriteBase /

RewriteEngine On
Options +Indexes
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^articles/([A-Za-z0-9-]+)$ articles.php?a_id=$1 [L]
RewriteRule ^articles/([A-Za-z0-9-]+)/$ articles.php?a_id=$1 [L]
RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]

Well, I tried them both. Your last answer gave me a 404 page and the answer before that prevented the article from being indexed in my front page. So, I could not even click on the links.

Ok try this;

    RewriteEngine On
    Options +Indexes
    Options +FollowSymlinks
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
    RewriteCond %{REQUEST_FILENAME} -d [NC]
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^articles/([A-Za-z0-9-]+)$ articles.php?a_id=$1 [L]
    RewriteRule ^articles/([A-Za-z0-9-]+)/$ articles.php?a_id=$1 [L]
    RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]

And then try typing the url manualy;

site.com/articles/article_title

Or Article id;

site.com/articles/2322

And let me know what the outcome is.

It gave me 404 error, I even tried what you said. Btw,is it possible to leave the user profile and replace that line for articles only? I rather have a better url for article than user profiles. I think, if we replace that line maybe something will happen. What I don't know is how the _ "underscores" come after each word...

I did this one to replace the user url-redirect code to article url re-write

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ articles.php?a_id=$1 [QSA,L]

But, it does not work. It should work right?

It should work if your php code translates the urls;

from; articles.php?a_id=12322

To; articles/article_title

Are you using a cms like WordPress or a forum like phpBB or is it writen in PHP by yourself?

What happens if you try this;

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#RewriteRule ^articles/([A-Za-z0-9-]+)$ articles.php?a_id=$1 [QSA,L]
RewriteRule ^articles/([A_Za_z0_9_]+)$ articles.php?a_id=$1 [QSA,L]
RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]

or this;

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^articles/(.*)$ articles.php?a_id=$1 [QSA,L]
#RewriteRule ^articles/([A_Za_z0_9_]+)$ articles.php?a_id=$1 [QSA,L]
RewriteRule ^(.*)$ ref.php?user=$1 [QSA,L]
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.