yab86
yab86

Reputation: 405

CodeIgniter htaccess remove query string from URL

I have would like to change the url mysite.com/page?id=s3q4afas to mysite.com/page/s3q4afas with htaccess.

What I have until now:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]


RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([^&]*) [NC]
RewriteRule ^ %{REQUEST_URI}page/%1 [R,L]
</IfModule>

Can somebody help me?

Upvotes: 0

Views: 1154

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following .htaccess :

<IfModule mod_rewrite.c>


    RewriteEngine on
    ##1)Redirect "/page/?id=foo" to "/page/foo"##
    RewriteCond %{THE_REQUEST} /page/?\?id=([^\s]+) [NC]
    RewriteRule ^ /page/%1? [L,R]
    ##2)internally redirect "/page/foo" to "/page/?id=foo"##
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^page/([^/]+)/?$ /page/?id=$1 [NC,L]
    ##3)rewrite any other non-existent request to "index.php"##
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$1 [PT,L]
</IfModule>

Upvotes: 1

Sparky
Sparky

Reputation: 98738

You would not need to alter the .htaccess file for this. CodeIgniter's URLs are segmented by default, unless you've intentionally set the enable_query_strings configuration option to TRUE.

By default, URLs in CodeIgniter are designed to be search-engine and human friendly. Rather than using the standard “query string” approach to URLs that is synonymous with dynamic systems, CodeIgniter uses a segment-based approach:

example.com/news/article/my_article

SEE: codeigniter.com/user_guide/general/urls.html

Enabling Query Strings

In some cases you might prefer to use query strings URLs:

index.php?c=products&m=view&id=345

CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you’ll see these items:

$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

If you change enable_query_strings to TRUE this feature will become active. Your controllers and functions will then be accessible using the “trigger” words you’ve set to invoke your controllers and methods:

index.php?c=controller&m=method

Upvotes: 0

Related Questions