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 
7
  • since when are spaces allowed in url's? Should that not be %20? Commented Dec 1, 2014 at 23:23
  • @PeterVR it is possible to have something like ?name=real estate or /real estate/ in the request. Commented Dec 2, 2014 at 1:16
  • Sure, but that should get url encoded to ?name=real%20estate or ?name=real+estate stackoverflow.com/questions/497908/… Commented Dec 2, 2014 at 1:28
  • @PeterVR then real+estate would be in the search box that I do not want. Commented Dec 2, 2014 at 1: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 Commented Dec 2, 2014 at 1:39

2 Answers 2

3
+50

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
1

You want to use the NE RewriteRule flag:

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

1 Comment

did not help it is still the same/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.