Ad

Slash (/) In GET-Request And My Mod_rewrite Statement In Htaccess

- 1 answer

I am getting a little bit crazy on the topic and hope to find some help.

I am re-building currently my website into an MVC structure. This includes also SEO-friendly (pretty) URLs.

I achieved already the transformation of my URL-requests

from: http://www.example.com/company?id=about_us
  to: http://www.example.com/company/about_us 

my .htaccess-file

RewriteEngine On
RewriteBase /

# Transforms an ugly-URL into a pretty-URL ('external redirect' updates also adress in browser)
#    ugly URL: www.example.com/company?id=about_us
#  pretty URL: www.example.com/company/about_us

RewriteCond %{QUERY_STRING} ^id=([\w-]+)$
RewriteRule ^(.+)$ $1/%1? [R=301,L]

# Transform an pretty-URL into a ugly-URL ('internal redirect')
#  pretty URL: www.example.com/company/about_us
#    ugly URL: www.example.com/index.php?url=company/about_us

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Now the GET-Request of a FORM (select box for some articles) comes into the game, which is not working with the above-mentioned htaccess-file. While each article has a SEO_slug saved in the database, which is dynamically put into the form. The SEO-slug has already this format:

"<city>/<type>/<articlename>"

The HTML looks like this:

<form method='get' action='../articles/'>
  <select name='id'>
     <option value='london/fruit/article_1' >Article 1</option>
     <option value='london/nuts/article_2'  >Article 2</option>
     <option value='newyork/fruit/article_3'>Article 3</option>
     <option value='newyork/nuts/article_4' >Article 4</option>
     <option value='miami/fruits/article_5' >Article 5</option>
  </select>
</form>

The Problem:

Now, the request is sent to the server, but the slashes (/) are transformed to '%2f', which generates with my current htaccess a server internal error.

Questions

1) Can I prevent the transformation from slash (/) to '%2f'?

2) How do I have to update my mod_rewrite to enable this. I have seen so many websites, but I never found a good solution. I was capable to achieve a bit with this:

RewriteCond %{QUERY_STRING} ^id=([\w-]+)(%2F*)(.*)(%2F*)(.*)$
RewriteRule ^(.+)$ $1/%1/%3/%5? [R=301,L]

, but I have problems with the numbers of slashes as sometimes the depth is different.

Can anybody give me a good advice? Many thanks! Maybe I am trying to solve the story on the wrong end and need to think totally different??

Cheers Tim

Ad

Answer

Finallay I found a solution, which I would like to share with you:

RewriteEngine On
RewriteBase /

# Transforms an ugly-URL into a pretty-URL ('external redirect' updates also adress in browser)
#    ugly URL: www.example.com/company?id=about_us
#  pretty URL: www.example.com/company/about_us
#
# REMARK: The important group is the thrid. The two first groups are
#         important if the query string contains slahes (/) that are
#         encoded as '%2F'. As there is no 'replace'-function each
#         Level of '%2F' needs to be rewritten step by step

RewriteCond %{QUERY_STRING} ^id=([\w-]+)%2F([\w-]+)%2F([\w-]+)$
RewriteRule ^(.+)$ $1/%1/%2/%3? [R=301,L]

RewriteCond %{QUERY_STRING} ^id=([\w-]+)%2F([\w-]+)$
RewriteRule ^(.+)$ $1/%1/%2? [R=301,L]

RewriteCond %{QUERY_STRING} ^id=([\w-]+)$
RewriteRule ^(.+)$ $1/%1? [R=301,L]

# Transform an pretty-URL into a ugly-URL ('internal redirect')
#  pretty URL: www.example.com/company/about_us
#    ugly URL: www.example.com/index.php?url=company/about_us

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

If you have any comment on this to improve it, I am happy to hear about it.

Ad
source: stackoverflow.com
Ad