So I've been trying to rewrite my php URLs with .htaccess so that they are more SEO/user friendly. Here is my code:
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
What this does is, it turns this:
http://www.example.com/?lang=en
http://www.example.com/contact?lang=en
Into this:
http://www.example.com/en/
http://www.example.com/en/contact
The problem:
http://www.example.com/en/
is not the same thing as:
http://www.example.com/en
And I get a 404 Not Found
error when I try to point my browser's address to http://www.example.com/en
. How can I make both versions of this work?
Adding DirectorySlashes On
to my root
.htaccess did not work. But my mobile version located in the /m/
folder has the following .htaccess:
# Set dir index
DirectoryIndex index.php
# Set dir slashes
DirectorySlash On
And it works! When I visit http://www.example.com/m
I get redirected to http://www.example.com/m/
. That is the wanted behavior. I guess it works because /m/
is a sub directory...
The question:
How can I make http://www.example.com/en
redirect to http://www.example.com/en/
without breaking the rest of my URLs?
Edit (my entire .htaccess
):
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
# Disable directory views
Options All -Indexes
IndexIgnore *
# Follow Symbolic links
Options +FollowSymLinks -MultiViews
# Create Directory Slashes
DirectorySlash On
# Rewrite urls
<IfModule mod_rewrite.c>
RewriteEngine on
# Pretty desktop URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/prices$ /prices?lang=$1 [L]
RewriteRule ^([^/]*)/offers$ /offers?lang=$1 [L]
RewriteRule ^([^/]*)/maps$ /maps?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
RewriteRule ^([^/]*)/links$ /links?lang=$1 [L]
RewriteRule ^([^/]*)/error$ /error?lang=$1 [L]
RewriteRule ^([^/]*)/verify-new$ /verify-new?lang=$1 [L]
RewriteRule ^([^/]*)/verify-old$ /verify-old?lang=$1 [L]
RewriteRule ^en/ski-rent-prices$ /ski-rent-prices [L]
# Rewrite mobile URLs
RewriteRule ^([^/]*)/m/$ /m/?lang=$1 [L]
# WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite URL without file extensions
# For .PHP files
RewriteRule ^error$ error.php [L]
RewriteRule ^offers$ offers.php [L]
RewriteRule ^prices$ prices.php [L]
RewriteRule ^maps$ maps.php [L]
RewriteRule ^contact$ contact.php [L]
RewriteRule ^links$ links.php [L]
RewriteRule ^verify-new$ verify-new.php [L]
RewriteRule ^verify-old$ verify-old.php [L]
# For .PDF files
RewriteRule ^ski-rent-prices$ ski-rent-prices.pdf [L]
</IfModule>
# Add Custom Error pages
ErrorDocument 400 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
# Default Character set
IndexOptions Charset=UTF-8
AddDefaultCharset UTF-8
# Hide the server signature
ServerSignature Off
<IfModule mod_rewrite.c>
# Add correct content types for documents
# JS:
AddType text/javascript .js
# CSS:
AddType text/css .js
# TXT:
AddType text/plain .txt
</IfModule>
<IfModule mod_headers.c>
# IE Compability Mode
BrowserMatch (MSIE|Trident) ie
Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
# Disallow 3rd party iframes
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
答案 0 :(得分:1)
RewriteCond %{QUERY_STRING} (^|&)lang=en($|&)
RewriteRule ^$ /en/? [L,R]
答案 1 :(得分:1)
更新了测试代码
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L,R=301]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L,R=301]
请参阅浏览器地址栏并检查重定向是否正确。如果错误提供源链接和重定向链接。如果没问题,请使用以下代码。
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L]
我已尝试使用以下链接的代码,并且其工作正常。
http://www.example.com/en
http://www.example.com/en/
http://www.example.com/en/contact
http://www.example.com/en/contact/
http://www.example.com/en/contact/m
http://www.example.com/en/contact/m/
http://www.example.com/en/prices
http://www.example.com/en/prices/
http://www.example.com/en/prices/m
http://www.example.com/en/prices/m/
自己检查这些链接以及其他链接。