1

htaccess

Options +FollowSymLinks
RewriteEngine on

# RewriteCond %{HTTP_HOST} !^example.com$ [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteRule ^rewrite-php-page/$ /example.com/rewrite-php-page.php [L]
RewriteRule ^rewrite-php-page-([^-]*)-get-input-query-([^-]*)/$ /example.com/rewrite-php-page.php?firstquery=$1&get-input-query=$2&submit=Send+values [L]

Rewriterule ^sitemap.xml$ sitemap-xml.php [L]

Options -Indexes

ErrorDocument 403 /error_pages/403.php
ErrorDocument 404 /error_pages/404.php

I'm trying to rewrite my site urls seo friendly. When I access my site url like this

http://www.example.com/rewrite-php-page/

This rewritten url also working fine when I directly access this url http://www.example.com/rewrite-php-page-123-get-input-query-456/ . But, when I submit a form with user inputs it doesn't redirect to this url

http://www.example.com/rewrite-php-page-123-get-input-query-456/

instead of this above url it normaly print the parameters in url like this

http://www.example.com/rewrite-php-page.php?firstquery=111&get-input-query=222&submit=Send+values

What should I do if I want to get the proper clean url when I'm submitting a form ?

Karuppiah RK
  • 3,761
  • 8
  • 33
  • 70
  • `get-input-query-456` is not more "clean" than just using proper query strings; it's more long-winded; irrelevant to Google (which the clean URL craze is passeewise about). -- If you feel like it, you'd need to manually assemble the target path on form submits per JavaScript / or implement a redirect to the supposed cleaner URLs in PHP. – mario Nov 29 '14 at 22:27
  • Relevantly related: [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/q/20563772) – mario Nov 29 '14 at 22:28

1 Answers1

1

Have your .htaccess like this:

ErrorDocument 403 /error_pages/403.php
ErrorDocument 404 /error_pages/404.php
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /example.com/

RewriteCond %{THE_REQUEST} /rewrite-php-page\.php\?firstquery=([^\s&]+)&get-input-query=([^\s&]+) [NC]
RewriteRule ^ rewrite-php-page-%1-get-input-query-%2/? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /rewrite-php-page\.php\s
RewriteRule ^ rewrite-php-page/ [L,R=302]

RewriteRule ^rewrite-php-page/?$ rewrite-php-page.php [L,NC]
RewriteRule ^rewrite-php-page-([^-]*)-get-input-query-([^/-]*)/?$ rewrite-php-page.php?firstquery=$1&get-input-query=$2&submit=Send+values [L,QSA,NC]

Rewriterule ^sitemap\.xml$ sitemap-xml.php [L,NC]
  • You need to place this in /example.com/.htaccess file.
  • Better to turn off MultiViews options to avoid content negotiation overriding mod_rewrite.
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • still it doesn't redirect the url properly. when I press a submit a button it display like this http://www.example.com/rewrite-php-page.php?firstquery=111&get-input-query=222&submit=Send+values – Karuppiah RK Nov 30 '14 at 08:26
  • when I use `RewriteBase /` in .htaccess file `localhost/example.com/sitemap.xml` sitemap.xml file doesn't open. but, when I remove `RewriteBase /` from .htaccess file it works. why? – Karuppiah RK Nov 30 '14 at 08:32
  • example.com is my main folder name. all php files and css,js,images folders are under example.com folder. .htaccess, robots.txt are also inside the folder – Karuppiah RK Nov 30 '14 at 08:36
  • it works now. how to add a slash (/) after `get-input-query-456` http://www.example.com/rewrite-php-page-123-get-input-query-456 – Karuppiah RK Nov 30 '14 at 08:58