animesh manglik
animesh manglik

Reputation: 755

.htaccess rewriting url for 2 different php

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.

Upvotes: 1

Views: 90

Answers (1)

anubhava
anubhava

Reputation: 786091

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]

Upvotes: 1

Related Questions