Barkermn01
Barkermn01

Reputation: 6822

Mod Rewrite Can't get it to work

I have a Mod Rewrite rule from my existing system

#default Lightweight rewriting
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php [L]

Now we have just purchased a new domain name allheavy.net and i want to use it as a url shortner as with the current system i have this band page

http://allheavymetal.net/band/band/id/222/name/Metallica

Now i want it so i can go to http://allheavy.net/Metallica

UPDATE 2 So this is what i have go so far

# Match the host, optionally with www.
RewriteCond %{HTTP_HOST} ^(www\.)?allheavy\.net$
# Ensure the rule doesn't apply to itself
RewriteCond %{REQUEST_URI} !/index/short
RewriteRule ^(.*)$ /index/short/d/$1 [L]

#Lightweight MVC
RewriteCond %{HTTP_HOST} ^(www.)?allheavymetal\.net$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

So from this the first rule works and the second rule works independantly

However if the first rule is called the rewritten url (/index/short/d/Metallica) but this is then not used when calling the second rule...

Upvotes: 1

Views: 91

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270599

You can combine your original two RewriteCond into one by making the www. optional with ()?. Then you'll need an additional condition to make sure the subsequent RewriteRule does not match the processing script URI /index/short/d.

# Match the host, optionally with www.
RewriteCond %{HTTP_HOST} ^(www\.)?allheavy\.net$
# Ensure the rule doesn't apply to itself
RewriteCond %{REQUEST_URI} !/index/short
# This should not match a leading / in directory context
# Use the [L] flag instead of the [C] chain flag to allow
# further rules to match if this does not
RewriteRule ^(.*)$ /index/short/d/$1 [L]
# Or if the same index.php will process this, omit [L]
# allowing the rule to continue execution and match the catch-all
# RewriteRule ^(.*)$ /index/short/d/$1

# Then place your original generic catch-all rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

All of this assumes that the script at allheavy.net/index/short/d is already able to successfully process and serve the target script via its URL shortener.

Some additional research

RewriteCond %{HTTP_HOST} ^(www\.)?allheavy\.net$
RewriteCond %{REQUEST_URI} !/index/short
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index/short/d/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

The above will first rewrite through /index/short/d/$1 and then into index.php, however, the controller URI /index/short/d/xxxxx is received by PHP in $_SERVER['REDIRECT_URL']. If your controller is able to make use of that, this might work for you.

Upvotes: 1

Related Questions