Jump to content

Leading Community for Usability, Search Engine Marketing,
Social Networking, Site Planning & Web Site Development, Since 1998


Photo

Mod_Rewrite Help Please! An Easy Fix I'm Sure...

apache htaccess

  • Please log in to reply
6 replies to this topic

#1 TheAlex

TheAlex

    Whirl Wind Member

  • Members
  • 61 posts
  • Twitter:alexofharford
  • Facebook:thecakesband

Posted 07 December 2012 - 12:20 PM

I'd like anything in the following format:
http://www.example.co.uk/photos.php?main=romania&sub=transylvania

To show as:
http://www.example.co.uk/photos/romania/transylvania

Here's my code (the 301 to www. works fine):

RewriteEngine On
Options +FollowSymLinks
RewriteCond %{http_host} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]

RewriteRule ^photos/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ photos.php?main=$1&sub=$2

I wondered if there's a conflict because my page is called photos.php and the start of the rule is 'photos'? I've tried a different filename but it still didn't work. I've looked at various guides online and still find myself stuck.

Thanks for any help. :)

Edited by TheAlex, 07 December 2012 - 04:11 PM.


#2 tam

tam

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 1824 posts

Posted 07 December 2012 - 11:45 PM

It's not something I'm familiar with but is it the trailing slash? It looks like you are looking for it to end in a slash but your eg doesn't have a slash after Transylvania.

#3 TheAlex

TheAlex

    Whirl Wind Member

  • Members
  • 61 posts
  • Twitter:alexofharford
  • Facebook:thecakesband

Posted 08 December 2012 - 03:49 AM

That still doesn't work, but you're right that I don't want it to end with a stroke.

Revised to:
RewriteRule ^photos/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ photos.php?main=$1&sub=$2


#4 glyn

glyn

    Sonic Boom Member

  • 1000 Post Club
  • 1855 posts

Posted 08 December 2012 - 07:59 AM

My advice is write to your host and ask, no server is the same.

#5 TheAlex

TheAlex

    Whirl Wind Member

  • Members
  • 61 posts
  • Twitter:alexofharford
  • Facebook:thecakesband

Posted 09 December 2012 - 07:29 AM

So here's the face-palm of the day...I contacted my host and tam's suggestion had fixed it, but I was expecting it to redirect automatically and hadn't checked the reformed link. Oops. I've only used the easy rewrite module in IIS before so that's my excuse. ;)

#6 tam

tam

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 1824 posts

Posted 09 December 2012 - 10:51 AM

Glad it worked :D

I think this line
[a-zA-Z0-9]
is saying letters and numbers only, which will need changing if you want to use a hyphen too. Googling, I think swapping to this might do it...

[a-zA-Z0-9-]

#7 TheAlex

TheAlex

    Whirl Wind Member

  • Members
  • 61 posts
  • Twitter:alexofharford
  • Facebook:thecakesband

Posted 05 January 2013 - 07:42 AM

I have another quick question on this. I've decided it'd be easier to code the website if I included the ID. The reason I didn't before is because I didn't want a number in the URL.

Is it possible to have
http://www.example.co.uk/photos.php?main=romania&sub=transylvania?id=100

Show as:
http://www.example.co.uk/photos/romania/transylvania
while still being able to access the ID querystring?

I've set my rule as:
RewriteRule ^photos/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)(.*)$ photos.php?main=$1&sub=$2&id=$3
but can't access the ID from the PHP.

I know that won't work because the rewrite makes it look like the ID is part of the 'sub' querystring, but if there's a way to do it without there being a stroke at the end of the URL I'd love to know.

Thanks again.

Edited by TheAlex, 05 January 2013 - 07:48 AM.






Also tagged with one or more of these keywords: apache, htaccess

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users