Vince Carter February 2016

.htacces url_rewrite difficulties

I have a problem with the configuration of the .htaccess of small website that I'm working on.

I want all pages to be redirected to index.php?page=REQUEST and that file will find in the database the content for the requested page.

The problem occurs when I have installed a forum, so I want these forum pages to redirect to the index.php?page=forum&params

Options +FollowSymlinks

RewriteEngine on 
RewriteCond %{REQUEST_URI} /(.*).html

RewriteRule ^(.*)forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L]

RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L]

Evetything works fine, except the forum part. How do I need to change the .htacces?

Answers


hillsy February 2016

The problem appears to be that your RewriteCond is matching requests that end in .html. As your forum URLs don't end in .html the condition for the subsequent RewriteRule is never met.

There are some other possible problems too:

  • ^(.*)forum will match www.url.com/en/ when it looks like you probably just want en

  • category/(.*) will match any characters, including forward slashes and the like. Presumably you just want it to match a decimal identifier.

  • Links to things that aren't covered by your rewrite config e.g. images

I'd probably rewrite your config to look something like this (N.B. not tested in Apache; only in a regex debugger):

RewriteEngine on    

# only match forum URLs
# e.g                url.com/en/forum/category/12345
RewriteCond %{REQUEST_URI} ^/.+/forum/category/[0-9]+
RewriteRule ^/(.+)/forum/category/([0-9]+) index.php?page=forum&lang=$1&category=$2 [L]

# match all URLs ending in .html
# e.g.            url.com/en/foo.html
# and                url.com/foo.html
RewriteCond %{REQUEST_URI} ^/.+\.html$

# a bit complicated, this matches both
#                      /apage.html
#               /folder/apage.html
RewriteRule ^(?:/(.+))?/(.+)\.html$ index.php?lang=$1&page=$2 [L]

The second RewriteRule should always provide a value for page but only provide a value for lang if the URL is of the form /lang/page.html. This should be OK if your index.php file can accept an empty lang parameter or supply a default value.

Alternatively, if you don't mind keeping your existing regex and it's only images, CSS etc you want to bypass in URL rewriting you can add some rules at the start to skip them e.g.

RewriteEngine on    

# don't actually rewrite, and stop processing rules
RewriteRule \.(jpg| 


Vince Carter February 2016

RewriteEngine on 

RewriteRule \.(jpg|png|gif|svg|css|js)$ - [L]

RewriteRule ^(.*)/forum/topic/(.*)?$ index\.php?page=forum&lang=$1&topic=$2 [L]
RewriteRule ^(.*)/forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L]

RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L]

Post Status

Asked in February 2016
Viewed 2,907 times
Voted 7
Answered 2 times

Search




Leave an answer