使用 htaccess 的 codeigniter seo url
codeigniter seo url with htaccess
我正在使用 Codeigniter 3.0.3
我需要这样使用link
http://www.example.com/my-controller/my-function/my-value1/other-value2/other-value3/value4
我有这个 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/?$ [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ [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/ [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/ [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
现在我需要将 - 写到 _
所以真正的url会是这样
/my_controller/my_function/my_value1/other_value2/other_value3/value4
我该怎么做?
在您的 config/routes.php 文件中:
你可以这样定义URL:
/my-controller/my-function/(:any)/other_value2/other_value3/value4
作为:
$route['my-controller/my-function/(:any)/(:any)/(:any)'] = "my_controller/my_function///";
在这里,这 3 个 (:any)
表示您的三个参数剩余 url 您可以使用任何东西。
再举一个简单路线的例子:
$route['about-us'] = "aboutus";
这里 aboutus
是我的控制器名称,我想访问它
http://localhost/project/about-us
我可以这样使用。
旁注: 您可以使用 uri->segment()
.
获得 URL 值
我正在使用 Codeigniter 3.0.3
我需要这样使用link
http://www.example.com/my-controller/my-function/my-value1/other-value2/other-value3/value4
我有这个 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/?$ [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ [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/ [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/ [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
现在我需要将 - 写到 _ 所以真正的url会是这样
/my_controller/my_function/my_value1/other_value2/other_value3/value4
我该怎么做?
在您的 config/routes.php 文件中:
你可以这样定义URL:
/my-controller/my-function/(:any)/other_value2/other_value3/value4
作为:
$route['my-controller/my-function/(:any)/(:any)/(:any)'] = "my_controller/my_function///";
在这里,这 3 个 (:any)
表示您的三个参数剩余 url 您可以使用任何东西。
再举一个简单路线的例子:
$route['about-us'] = "aboutus";
这里 aboutus
是我的控制器名称,我想访问它
http://localhost/project/about-us
我可以这样使用。
旁注: 您可以使用 uri->segment()
.