0

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?

1
  • You skipped the URL section of the docs. You don't need to mess with .htaccess for this! By default, query strings are disabled in CodeIgniter. Just make sure that CodeIgniter's enable_query_strings configuration option is set to FALSE. Commented May 2, 2016 at 19:27

2 Answers 2

1

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

3 Comments

Maybe after page slash should be avoided because there is no any in his example. Or is that already made by these codes /page/?\?id=([^\s]+) and /page/?id=$1?
Why use .htaccess for this when a simple CodeIgniter configuration option determines the URL format? (By default, query strings are disabled in CodeIgniter.)
Works perfectly, but now I get the 404 page. :-( I do not like enable the query strings per default!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.