2

I am updating my question according to suggestion below

My .htaccess file code for rewrite -

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L]

</IfModule>

But above .htaccess rule is not changing my url . If you go to url below it is not rewriting url as per rule

Url - http://expertsusa.org/SearchResult.php?category_slug=t

Url Should be rewritten to http://expertsusa.org/SearchResult/t

  • Possible duplicate of [URL rewriting with PHP](http://stackoverflow.com/questions/16388959/url-rewriting-with-php) – TaZz Mar 30 '16 at 10:38
  • @TaZz I don't think its duplicate . I have seen the link you provided and tried the solution over there but mine one is different thing . If you see my first rewrite rule i am already removing .php from url its xyz.com/SearchResult not searchresult.php so I need to make all links without php and also I want to Add Parameter to url like SearchResult/Keyword –  Mar 30 '16 at 11:42
  • url is here - http://expertsusa.org/SearchResult –  Mar 30 '16 at 11:42
  • Do you want `CategoryName` in URL to be passed as GET parameter with name as `category_slug` to `SearchResult.php`? – anubhava Mar 30 '16 at 14:46
  • @anubhava, I want to convert SearchResult.php to SearchResult along with one single parameter –  Mar 30 '16 at 17:15
  • What its doing it - SearchResult/CategoryName –  Mar 30 '16 at 17:16
  • what it exactly doing is SearchResult/?Category=name –  Mar 30 '16 at 17:16
  • @steve Can you please come in disussion – Heemanshu Bhalla Mar 31 '16 at 06:57

3 Answers3

0

Order is important

<IfModule mod_rewrite.c>
RewriteEngine On

# browser requests PHP

RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

#It is to check whether Request if for PHP File
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

</IfModule>
Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53
Abu Musab
  • 96
  • 6
  • If you see my first rewrite rule i am already removing .php from url its xyz.com/SearchResult not searchresult.php so I need to make all links without php and also I want to Add Parameter to url like SearchResult/Keyword –  Mar 30 '16 at 11:46
  • RewriteRule ^searchresult/(.*)$ searchresult.php?category_slug=$1 [QSA,L] Use the above rule. Your url will look like: http://domain.com/searchresult/pets – Abu Musab Mar 30 '16 at 11:48
  • i am using RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L] rule as you said but my resulting url is http://expertsusa.org/SearchResult.php?category_slug=234567 –  Mar 30 '16 at 11:55
  • Resulting url should be: expertsusa.org/SearchResult/234567 Post your .htacess file – Abu Musab Mar 30 '16 at 11:56
  • my .htaccess file ------# browser requests PHP RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L] RewriteRule ^/?(.*)\.php$ /$1 [L,R=301] ----------- its saying too many redirects you can check online –  Mar 30 '16 at 12:04
  • you can check online here - http://expertsusa.org/SearchResult.php?category_slug=4 i will catch you up in 1 hr –  Mar 30 '16 at 12:05
0

Try these rules:

Options -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} /SearchResult\.php\?category_slug=([^\s&]+) [NC]
RewriteRule ^ /SearchResult/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

RewriteRule ^SearchResult/(.*)$ SearchResult.php?category_slug=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • No its not working except homepage no other page is opening blank page on all other pages –  Mar 31 '16 at 05:53
  • Exact same rule in my Apache redirects `localhost/SearchResult.php?category_slug=t` to `localhost/SearchResult/t` Is that not what you expect? Make sure you don't have any rule other than what I've shown in answer and this is located in site root .htaccess. – anubhava Mar 31 '16 at 06:11
  • Do you mean to put exactly as you given –  Mar 31 '16 at 06:15
  • it rewriting my url but throwing it on 404 not found page http://expertsusa.org/SearchResult/5 –  Mar 31 '16 at 06:17
  • I update .htacces with your rules you can see online - expertsusa.org but except homepage all pages are 404 –  Mar 31 '16 at 06:18
  • So redirect rule is working fine but `http://expertsusa.org/SearchResult/t` is not loading correct content. Where is `SearchResult.php` located? – anubhava Mar 31 '16 at 06:22
  • its in website folder . if i remove the rules it will work fine with searchresult.php and load result depending on values posted from back page –  Mar 31 '16 at 06:25
  • i think it is not finding any folder named t in SearchResult folder that's why its giving 404 but it should not do this –  Mar 31 '16 at 06:26
  • Is `SearchResult` name of a real directory? Can you add `ErrorDocument 404 default` at top of your .htaccess and retry – anubhava Mar 31 '16 at 06:27
  • SearchResult is not a folder . SearchResult.php is file with SearchResult?Category_slug=t that should be rewritting to SearchResult/t going to add this line on top of .htaccess ErrorDocument 404 default –  Mar 31 '16 at 06:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107812/discussion-between-anubhava-and-steve). – anubhava Mar 31 '16 at 06:30
0

First of All Try the Code Below in your .htaccess file -

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule    ^/SearchResult/$1   SearchResult.php?id=$1    [NC,L]    
</IfModule>

The code Above is for SearchResult.php Page For adding more pages you can undrestand and use similar type of code above like for About.php you can use below -

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteRule    ^/SearchResult/$1   SearchResult.php?id=$1    [NC,L]    

RewriteRule    ^/About/$1   About.php    [NC,L]    

</IfModule>

In You Case your urls will work as you need after above changesin .htaccess file . But your page css is fumbled after this as you js and specially your css files are not having root path try solution below -

Convert following code lines

 <link href="css/bootstrap.min.css" rel="stylesheet" async>
    <script src="js/jquery.min.js" ></script>
    <script src="js/bootstrap.min.js" ></script>
    <script src="js/alertify.min.js" type="text/javascript" ></script>

To Below

<link href="/css/bootstrap.min.css" rel="stylesheet" async>
<script src="/js/jquery.min.js" ></script>
<script src="/js/bootstrap.min.js" ></script>
<script src="/js/alertify.min.js" type="text/javascript" ></script>
Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53