Totally lost with friendly URLs

Hi everyone,

I have been trying to modify my URLs and adapt them to a friendlier way with no result. It is supposed that I should change it through .htaccess. My URLs are for example this way:

private/noticias.php?ID=158

And I would like to change them to something like this:

private/noticias/here-should-be-written-the-title

I have tried with the following code:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^private/noticias/([0-9]+)\.html$ private/noticias.php?ID=$1 [L]

Nothing happens. I have been talking with my hosting supplier but anything has been solved. They have told me that I have to set-up my friendly URLs first on my website, but hasn’t be done with the htaccess file?

Hope somebody could give me some tips.

Hi,
For your rule to work you should be requesting some URL finishing in .html like ‘private/noticias/here-should-be-written-the-title.html’. If you’re not happy with the .html bit then just remove it from the rule:

RewriteRule ^private/noticias/([0-9]+)$ private/noticias.php?ID=$1 [L]

For my web applications with clean URLs I have this simple htaccess rule:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L] 

What that does is redirect every request to your app into index.php. Architecturally this already gives you some advantages as you can run all common code and bootstrap your app from index.php.
The thing with clean URLs is that unless you want to bloat your htaccess with rules for your app, you need your application to feature a router, which will basically parse the URL and based on it feed you the appropriate content.

Hope that helps.

2 Likes

Thanks for your response,

I have tried with the code but it throws me to a 404 error page. I don’t really understand what is happening. I even can understand how if the URL is this way noticias/Ya-se-conocen-los-Games-With-Gold-de-junio htaccess will turn it to noticias.php?ID=20 for example. I mean, how can htaccess know which the ID is? Because in the friendly URL I am only showing the title of the news, not the ID itself.

I would understand if the friendly URL were: noticias/1 and htaccess translate it to noticias.php?ID=1, because in this case I am sending the ID.

Sorry for my explanations.

Yeah in that case htaccess would not know how to translate the article name into an id, your application would have to do that, and that goes back to the router and URL parsing I mentioned earlier. So in that page you would explode the URL, pick the last bit and look into your database for an article with that name, if it exists then render it otherwise give a 404 error

1 Like

By the way, just noticed as well that your rule will only accept numbers as parameter. So to accept letters and hyphens as well instead of ([0-9]+) do ([0-9a-zA-Z-]+) and then if that is the case you still have to deal with finding the correct article via its name in your app

1 Like

You are right, I have changed that part of the code. But I keep having problems to understand what exactly I have to do. In each friendly URL tutorial I have seen, they only explain that htaccess file has to be changed…nothing else.

So if you had a rule like so:

RewriteRule ^private/noticias/([0-9]+)$ private/noticias.php?ID=$1 [L]

and a URL like private/noticias/1 you would not have to do anything assuming that your application is picking up the ID from GET and looking for the article by id in the database.

However if you had a rule like so (which I think it is what you’re after):

RewriteRule ^private/noticias/([0-9a-zA-Z\-]+)$ private/noticias.php?name=$1 [L]

and a URL like private/noticias/una-noticia-asombrosa
you would have to pick up the ‘name’ parameter from GET and then grab your article from the database not based on article id but based on the ‘name’ variable ‘una-noticia-asombrosa’.
So htaccess cannot tell you what id that article has based on its name, it is entirely up to your app to figure that out. I hope this clarifies it and if you’re still in doubt don’t hesitate to ask.
Cheers

1 Like

I really appreciate your time. I have tried just with the iD example, and it “work”, not as it should. CSS styles are not working.

https://s21.postimg.org/oh61oint3/Captura_de_pantalla_2017-05-31_a_las_6.43.14.png

Going back to the case I would like to work with. How do I have to tell to htaccess that, that title of the URL, belongs to a “X” ID? Do I have to do it via PHP?

Thanks again for your time.

Add these before your rule in the htaccess, see if it helps with the CSS:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

You don’t have to tell htaccess anything with php. Just in php you select your article by name instead of by id. Thats why in my latest rule example i added the url param as ‘name’ instead of ‘ID’. So you can check for both. If you get an id then select by id, if you get a name then select by name.

So you can have 2 rules

RewriteRule ^private/noticias/([0-9]+)$ private/noticias.php?ID=$1 [L]
RewriteRule ^private/noticias/([0-9a-zA-Z\-]+)$ private/noticias.php?name=$1 [L]

then in php

$id = intval($_GET["ID"]);

if ($id) {
	// get article by id
} else if (isset($_GET["name"] && !empty($_GET["name"])) {
    //get article by name
    /*of course this is a little tricky as you will have to remove the hyphens from the string 
and replace them with spaces, run a case insensitive search on your article titles, and then possibly deal with the case that you may have two articles with the same name
    */
  
} 

// if failed to retrieve article return a 404 
1 Like

Thanks again, I have been making some trials, just with the ID. I keep having the problem that the CSS files are not charging.

If I enter in the URL manually the following:

http://localhost/xboxone/noticias/160/

It redirects me to

http://localhost/xboxone/noticias.php?ID=160

Which is correct, but if I press in the news link instead of showing a friendly URL it shows me the old one. I am using this rule:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /XboxOne/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

# Rewrite for the news
RewriteRule ^noticias/([0-9]+)/ noticias.php?ID=$1 [L]

EDIT: Is working fine the redirection, just having problems with the CSS, images and those files that are not charging because of de directory. Is there a way to solve this?

I have realized that all images or files, as they are set in a relative directory, don’t charge too. Do I have to change all of them? I am working in Localhost, if I have to change them I will have to change them again to upload to the server.

I think it would be better if you followed the standard pretty-urls path.

Use .htaccess file and point everything to index.php

RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php [QSA,L]

 # specific files and directories will still be available

Try this:
http://this-is-a-test-to-see-if-it-works.tk/index.php

Did you notice how the URL parameters were parsed:

 $params = $_SERVER['REQUEST_URI'];

 $val = explode('/', $params );

	echo '<pre>';
		echo '<b>Results:</b>';
		echo '<hr>';
		print_r($val); 
	echo '</pre>';
1 Like

It sends me always to index.php. I have done something, I have created a variable to use absolute directories. I am changing to each image, internal link. I don’t know if it is the best way but it is working, and when I upload it to the server the only thing I have to do is change that variable value.

How can I allow in htaccess some special characters? I also want to remove this but it is not working just replacing the game title with this:

$titulo_limpio = str_replace(",","-",$titulo_limpio);

If I remove that character manually the URL works. It’s just for the titles that have that thing. I am using:

RewriteRule ^Juegos/Xbox-One/([0-9]+)/([0-9a-zA-Z_-]+)/ single.php?ID=$1 [L]

I have tried also to allow that character in the URL but it’s not working.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.