1

I am using htaccess for clean url. Which is working fine as long as the parameters doesn't carry whitespace. In which case it converts the space in %20 and I receive 404 error.

So basically the URL: http://localhost:8080/series/dynamics/admin/cleanURL/green%20apple

gives me 404 error. But URL: http://localhost:8080/series/dynamics/admin/cleanURL/greenapple works fine.

Also is there a way I can remove the directory details from the URL, I tried

RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]

But doesn't work

htaccess

<IfModule mod_rewrite.c>
     Options +MultiViews 
     Rewriteengine on
     RewriteBase /series/dynamics/admin/cleanURL/
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
     Rewriterule ^([a-zA-Z0-9]+)/?$ index.php?product=$1
     RewriteRule ^(.*)(\s|%20)(.*)$ /$3-$4 [R=301,L,NE]
     #rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
     Rewriterule ^([a-zA-Z0-9]+(.*)+)/([^/]+(.*)+)/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]
     </IfModule>
Gautam P Behera
  • 171
  • 2
  • 13

1 Answers1

1

You will need to add this rule in your site root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) series/dynamics/admin/cleanURL/$1 [L]

Then have these rules in /series/dynamics/admin/cleanURL/.htaccess:

Options +MultiViews 
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/

RewriteCond %{THE_REQUEST} /series/dynamics/admin/cleanURL/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=302,NE]

RewriteRule ^([^\s\x20]*)[\s\x20]+(.*)$ $1-$2 [L,R=302,NE]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

Rewriterule ^(\w+)/?$ index.php?product=$1 [L,QSA]

#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*))/([^/]+(.*))/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • @anubhava..Thanks for looking into it. I have managed to get rid of directory from url. However when it encounters space the URL looks like: http://localhost:8080/-, while it sd be http://localhost:8080/green-apple. And I have another problem now as I am using php $_GET function to extract further data from the database using the parameter on the url I have completely lost that with this chnge – Gautam P Behera Feb 27 '16 at 02:43
  • Also when check further print_r($_SERVER); I am actually getting empty query string [QUERY_STRING] => [REQUEST_URI] => /apple [SCRIPT_NAME] => /series/dynamics/admin/cleanURL/index.php [PHP_SELF] => /series/dynamics/admin/cleanURL/index.php – Gautam P Behera Feb 27 '16 at 03:20
  • could you explain what RewriteRule ^ /%1 [L,R=302,NE] is actually doing ? – Gautam P Behera Feb 27 '16 at 09:49
  • sorry it looks like taking me forever to get around this. Now I have managed to successfully replace whitespace with hyphen with the help of above code. But how could I get the url to be like index.php?product=Green apple at the backend as php doesn't recognise Green-apple neither Green%20apple. Thanks in advance. – Gautam P Behera Feb 28 '16 at 10:12
  • Ok, I have done that. So now url has got hyphen and href value has whitespace. The variable in the database also has a white space. when I am hard coding the value my query is able to fetch further data but not from the URL. would you know any possible reason... – Gautam P Behera Feb 28 '16 at 10:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104773/discussion-between-gautam-p-behera-and-anubhava). – Gautam P Behera Feb 28 '16 at 10:28
  • when I am using these 2 lines RewriteRule ^ /%1 [L,R=302,NE] & RewriteRule ^ - [L] which is helping me to get rid of directory details I am loosing the functionality of my $_GET function. i.e. I cant fetch data from database cd you suggest any possible reason of why it may be occurring. – Gautam P Behera Feb 29 '16 at 16:06
  • One more query. RewriteCond %{THE_REQUEST} /series/dynamics/admin/cleanURL/(\S*)\s [NC] will this line will allow only one variable as per RewriteRule ^ /%1 [L,R=302,NE] , i.e. %1.? If yes how could I add another variable to it. as you cd see I have both product & sub-product in my URL – Gautam P Behera Feb 29 '16 at 16:13
  • The orginal URL is: http://localhost:8080/series/dynamics/admin/cleanURL/Pumps after using suggested quote it become:http://localhost:8080/Pumps. However I am not able to fetch further info related to "Pumps" here – Gautam P Behera Mar 01 '16 at 14:04
  • Ah I made a mistake. Try my updated rule in root .htaccess – anubhava Mar 02 '16 at 06:33
  • 1
    Thanks @anubhava :), I appreciate the patience you showed in solving this. – Gautam P Behera Mar 02 '16 at 13:18