Axiom
Axiom

Reputation: 902

.htaccess RewriteRule in subdirectory with PHP GET variables

I have two problems here pertaining to the .htaccess and PHP GET variables.

Basically, I'm working on an anime site, and I want to be able to load up site.com/anime/AnimeName

Right now, I have it as site.com/anime/index.php?watch=AnimeName

I was able to do this for my users profiles:

RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ /profile.php?username=$1 

I tried similar approaches with the .htaccess in the /anime/ subdirectory, but I can't seem to get it to work.

My second issue (isn't as big deal as the main issue), is I'd like to know how I can set TWO variables for .htaccess files.

Ex: I have episodes for site.com/anime/?watch=AnimeName&ep=Ep#

Basically, I want to know how I can get it to be like this: site.com/anime/AnimeName/1 for episode one, site.com/anime/AnimeName/2 for episode two, and so on.


UPDATE: I did this in my main .htaccess file and it works for both usernames and anime.

RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^u/(.*)$ /profile.php?username=$1 
RewriteRule ^a/(.*)$ /anime.php?watch=$1 

Now, how can I make it to where I can do this: site.com/a/AnimeName/1 for Episode 1?

I've tried this but it didn't work: RewriteRule ^(.*)$ /a/&ep=$1 Either way, Thanks in Advanced.

Upvotes: 1

Views: 1586

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

Rewrite conditions only get applied to the immediately following rule. So you need to duplicate the conditions.

RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^u/(.*)$ /profile.php?username=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^a/(.*)$ /anime.php?watch=$1 [L]

However, if /a/ and /u/ aren't real subdirectories, then you don't need the conditions. For the additional parameter, you need a 3rd rule:

RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^u/(.*)$ /profile.php?username=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^a/([^/]+)$ /anime.php?watch=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^a/([^/]+)/([0-9]+)$ /anime.php?watch=$1&ep=$2 [L]

Upvotes: 2

Related Questions