0

I am very new to apache and mod re-write I am trying to make pretty links using .htaccess. My URL looks like this:

http://example.com/single_picture.php?name=Testing-123&cat_id=1

I want to make it look like

http://example.com/pictures/Testing-123/

That is just the name. Here is what I have put in my .htaccess file but it is not working.

Options +FollowSymLinks
RewriteEngine On


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

    RewriteRule ^pictures/(\s+)*$ ./single_picture.php?name=$1&cat_id=$2

How do I do it? Ahmar

1 Answer 1

1

Try This:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^pictures/([A-Za-z0-9\-_]+)/([A-Za-z0-9\-_]+)/?$ /single_picture.php?name=$1&cat_id=$2

Also remember that a rewrite will not rewrite the request a user has made. i.e. http://example.com/single_picture.php?name=Testing-123&cat_id=1 will work as will http://example.com/pictures/Testing-123/1/.

You will need to make the user view the page via the SEO link by changing the links on your website to reflect this.

Sign up to request clarification or add additional context in comments.

6 Comments

Michael thanks for your answer. I replaced the code in my .htaccess with the one you provided but unfortunately it is not working as I expect. For example here is the working url unclelol.com/… but when I enter unclelol.com/pictures/Geo-news/1 it doesn't
Ok, this is not an issue with your .htaccess file. It is with the association to your css and js files in the <head> section of your website. I like to work from the root of my site when adding css and js link. So, to steal a bit of code from your website <link href="css/style.css" rel="stylesheet">, simply change it to <link href="/css/style.css" rel="stylesheet"> (Notice the leading slash). This tells the browser to start at the roor of the websites filesystem.
@AhmarAli Just another note. Don't forget to also apply this same rule to your images. / in front.
You have been great so far. Can you please tell me how do I make the format look like picture/category/and then picture name?
@AhmarAli I think this is what you were wanting. RewriteRule ^picture/category-([0-9]+)/([A-Za-z0-9\-_]+)/?$ /single_picture.php?cat_id=$1&name=$2 This will make http://example.com/picture/category-1/Testing-123/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.