1

I have two php mainly - share.php and gen.php

SHARE.PHP http://example.com/share.php?name=52afcb2793007

while

GEN.PHP http://example.com/gen.php?name=images/This%20is%20%20%20example.jpg

I am new to writing .htaccess and have been reading on stackoverflow and finding pretty url makers for .htaccess. So far I can either run share.php or gen.php My current .htaccess looks like this

ErrorDocument 404 /404.html
RewriteEngine on
RewriteRule ^new/?$ new.php  [NC]
RewriteRule ^webcam/?$ webcam.php  [NC]
RewriteRule ^([a-zA-Z0-9]+)$ share.php?name=$1
RewriteRule ^([a-zA-Z0-9]+)/$ share.php?name=$1
RewriteRule ^([a-zA-Z0-9]+)$ gen.php?name=images/$1
RewriteRule ^([a-zA-Z0-9]+)/$ gen.php?name=images/$1

It gives a 500 server error when I give a url like http://example.com/someimage.jpg which I guess is because its trying to access share.php and getting the error.

The output I am looking to get is something like this http://example.com/gen/Image.jpg (where Images will be hyphened instead of %20 - been reading on that as well) http://example.com/some_id (goes to share.php) - this works which I am guessing is the rule that .htaccess sees first and matches and the id exists so gives the result.

3
  • It's going to share.php because your patterns are exactly the same for both share and gen, so a request /blah matches both of them, thus the first rule gets executed first, always, no matter what. That shouldn't be causing the 500 server error though unless your php script is returning that. Commented Dec 17, 2013 at 4:48
  • So what changes can be done to gen.php so that it differs Commented Dec 17, 2013 at 6:37
  • one thing you can do is to preface it with a unique pattern, like /gen/(something), and your URLs will look like what you've linked: http://example.com/gen/Image.jpg, then "Image.jpg" gets passed into gen.php as name=images/Image.jpg Commented Dec 17, 2013 at 7:22

1 Answer 1

1

Keep your .htaccess like this:

ErrorDocument 404 /404.html
RewriteEngine on

RewriteRule ^new/?$ new.php [L,NC]

RewriteRule ^webcam/?$ webcam.php [L,NC]

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^(.+?\.jpe?g)/?$ gen.php?name=images/$1 [L,QSA]

RewriteRule ^([a-zA-Z0-9]+)/?$ share.php?name=$1 [L,QSA]
Sign up to request clarification or add additional context in comments.

7 Comments

I used the above .htacess and for some reason my css and js files are not being read.
That's a known issue. See my response here: stackoverflow.com/a/20616955/548225
wow...that worked.... but now the image url which i am getting from $phpvalue = $_GET["name"]; is images/gen/Actual Advice Mallard.jpg ... All my images are under /images, why is this gen being added. I can move the image folder under gen but I still dont get it
That is because you're using relative path. If you always use absolute path like /images/Actual Advice Mallard.jpg then your paths will never get messed up with any pretty URL schemes.
there is just one addon to this..I will not get a 404 error. Since now any url will be directed to share.php . Is there a specific way to direct only url with jpg ending, or should I check the get method in php and the redirect user to 404
|

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.