3

I need to update my .htaccess file for URL rewriting. Even this post did not help. I need to divert as following but it does not work!

First requirement

(Just alphabet - case insensitive - accept space)

domain.com/Acco unting > domain.com/edit.jsp?kazem=Acco unting&oto=1&sab=
domain.com/acco unting > domain.com/edit.jsp?kazem=acco unting&oto=1&sab=
domain.com/Accounting > domain.com/edit.jsp?kazem=Accounting&oto=1&sab=
domain.com/accounting > domain.com/edit.jsp?kazem=accounting&oto=1&sab=

Second requirement

(First part just alphabet,accept space - Second part alphabet and number, accept space)

domain.com/Accounting/city 2222 > domain.com/edit.jsp?kazem=Accounting&oto=1&sab=city 2222
domain.com/Accou nting/city 2222 > domain.com/edit.jsp?kazem=Accou nting&oto=1&sab=city 2222

My .htaccess is as following

    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
</IfModule>
#Options +FollowSymLinks
#IndexIgnore */*

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
RewriteRule ^(.*)/(.*[0-9]+)$ /edit.jsp?kazem$1&oto=1&sab=$2 [L]
</ifModule>

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
#RewriteRule ^(.[a-zA-Z]+)$ /edit.jsp?kazem=$1&oto=1&sab= [L]
RewriteRule "^([a-z]+( [a-z]+)?)/?$" /edit.jsp?kazem=$1&oto=1&sab= [L]

</ifModule>

IndexIgnore *

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
#  Header set Access-Control-Allow-Credentials true
  Header add Access-Control-Allow-Headers "*,origin, x-requested-with, content-type"
  Header add Access-Control-Allow-Methods "GET, POST, DELETE, OPTIONS"
</IfModule>


DirectoryIndex index.html index.php
Community
  • 1
  • 1
Jack
  • 6,430
  • 27
  • 80
  • 151
  • since when are spaces allowed in url's? Should that not be `%20`? – Pevara Dec 01 '14 at 23:23
  • @PeterVR it is possible to have something like ?name=real estate or /real estate/ in the request. – Jack Dec 02 '14 at 01:16
  • Sure, but that should get url encoded to ?name=real%20estate or ?name=real+estate http://stackoverflow.com/questions/497908/is-a-url-allowed-to-contain-a-space – Pevara Dec 02 '14 at 01:28
  • @PeterVR then real+estate would be in the search box that I do not want. – Jack Dec 02 '14 at 01:35
  • Off course you don't want that, the browser will url encode it for you on submit of your form. Have a look at the request headers and see for yourself... And if you are using some fancy JavaScript to do the request you should encode it yourself, or k – Pevara Dec 02 '14 at 01:39
  • @PeterVR the one that anubhava offered worked for a while but for some reasons not now, do not know what the reason is. – Jack Dec 02 '14 at 01:47
  • @PeterVR even if there is space in the address that is associated to will be changed! what to do? – Jack Dec 02 '14 at 03:07

2 Answers2

3

This rule should work for you:

RewriteEngine On
RewriteBase /

RewriteRule "^([a-z]+( [a-z]+)?)/?$" edit.jsp?name=$1 [L,QSA,NC]
  • Don't use http:// in target URI otherwise it will become an external redirect with R=302
  • Quote the pattern while using space

Complete .htaccess:

IndexIgnore *    
DirectoryIndex index.html index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule ^([^/]+)/(\d+)/?$ edit.jsp?kazem$1&oto=1&sab=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule "^(\S+(?: \S+)*)/?$" edit.jsp?kazem=$1&oto=1&sab= [L,QSA]

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
  #  Header set Access-Control-Allow-Credentials true
  Header add Access-Control-Allow-Headers "*,origin, x-requested-with, content-type"
  Header add Access-Control-Allow-Methods "GET, POST, DELETE, OPTIONS"
</IfModule>
Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66180/discussion-between-anubhava-and-jack). – anubhava Dec 04 '14 at 06:08
  • See my chat invitation above. Better to resolve this in chat than in endless comments. I suspect your Java code has some issues that you need to fix. – anubhava Dec 04 '14 at 06:15
1

You want to use the NE RewriteRule flag:

RewriteRule ^([a-z\s]+)$ http://example.com/edit.jsp?name=$1 [NC,NE,L]
hjpotter92
  • 78,589
  • 36
  • 144
  • 183