codeigniter seo url with htaccess

- 1 answer

Ad

I`m using Codeigniter 3.0.3

I need to use link like this

http://www.example.com/my-controller/my-function/my-value1/other-value2/other-value3/value4

I have this htaccess

# Disable file indexing
Options -Indexes
# Follow symbolic links.
Options +FollowSymLinks

php_value upload_max_filesize 16M
php_value post_max_size 16M
php_value max_input_vars 1000000

# Compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# adding type for .woff2:
AddType application/x-font-woff2 .woff2
# adding type for .woff:
AddType application/x-font-woff .woff
AddType application/vnd.ms-word.document.macroEnabled.12 .docm
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
AddType application/vnd.ms-powerpoint.template.macroEnabled.12 potm
AddType application/vnd.openxmlformats-officedocument.presentationml.template potx
AddType application/vnd.ms-powerpoint.addin.macroEnabled.12 ppam
AddType application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsm
AddType application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
AddType application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptm
AddType application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
AddType application/vnd.ms-excel.addin.macroEnabled.12 xlam
AddType application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb
AddType application/vnd.ms-excel.sheet.macroEnabled.12 xlsm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
AddType application/vnd.ms-excel.template.macroEnabled.12 xltm

# Prevent viewing of htaccess file.
<Files .htaccess>
    order allow,deny
    deny from all
</Files>

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    # Set REQUEST_SCHEME (standard environment variable in Apache 2.4)
    RewriteCond %{HTTPS} off
    RewriteRule .* - [E=REQUEST_SCHEME:http]

    RewriteCond %{HTTPS} on
    RewriteRule .* - [E=REQUEST_SCHEME:https]

    # Check for POST Submission
    RewriteCond %{REQUEST_METHOD} !^POST$

    # Redirect to domain with www.
    RewriteCond %{HTTPS} off
    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{SERVER_NAME} !^www\. [NC]
    RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

    # Redirect to domain with www.
    RewriteCond %{HTTPS} on
    RewriteCond %{SERVER_PORT} 443
    RewriteCond %{SERVER_NAME} !^www\. [NC]
    RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

    ### Canonicalize codeigniter URLs

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(index(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    ###
    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php
</IfModule>

now I need to owerwrite - to _ so the real url will be like this

/my_controller/my_function/my_value1/other_value2/other_value3/value4

how can I do this?

Ad

Answer

Ad

In your config/routes.php file:

You can define this URL:

/my-controller/my-function/(:any)/other_value2/other_value3/value4

As:

$route['my-controller/my-function/(:any)/(:any)/(:any)'] = "my_controller/my_function/$1/$2/$3";

Here, these 3 (:any) means your three params rest url you can use any thing do you.

One more example with simple route:

$route['about-us'] = "aboutus";

Here aboutus is my controller name, and i want to access it as

http://localhost/project/about-us

Than i can use like that.

Side Note: You can get URL values by using uri->segment().

User Guide

Ad
source: stackoverflow.com
Ad