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>
Rewrite --- www.example.com/?lang=en => www.example.com/en/
RewriteCond %{QUERY_STRING} (^|&)lang=en($|&)
RewriteRule ^$ /en/? [L,R]
Updated code for testing
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]
See the browser address bar and check if the redirections are correct. If it wrong provide the source link and redirected link. If it is ok, Then use the below code.
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]
I have tried the code with the below links and its working fine.
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/
Check these links yourself and also other links as well.
Related
I have main domain http://example.com on my root directory and all of my subdomains are in catalogs folder /catalogs/subdomain1,/catalogs/subdomain2.Currently my subdomain url in http://subdomain.example.com/index.php
I want to remove index.php from my subdomain.
My Root .htaccess file code is
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect on https:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
My Subdomain .htaccess
RewriteEngine on
# Redirect on https:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Options -Indexes
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
Please use below code and update RewriteBase /root_directory/subdomain_folder_name/ at your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /root_directory/subdomain_folder_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
I hope this will helpful for you.
Try this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://subdomain.example.com/$1 [R=301,L]
RewriteRule ^$ folder [L]
DirectoryIndex index.php
I want to redirect blog.byperte.com/blog/article to blog.byperte.com/article. The blog is built on Anchor CMS and has the following .htaccess file:
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
To remove a file directory from a URL, use this in your .htaccess:
RewriteEngine On
RewriteRule ^blog/(.*)$ /$1 [L,R=301]
Make sure to clear your cache before you test it.
New .htaccess file (still not working, it sends me to localhost/xampp
RewriteEngine on
RewriteBase /sample/
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
Old .htaccess file below:
RewriteEngine on
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove index.php
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://localhost/sample/ [R=301,L]
Domain is http://localhost/sample/. I have tried many things that Google has given me but none of them work. Every time I go to localhost/sample/index.php it doesn't redirect to localhost/sample/. I also used the Redirect 301 /index.php /.
I tried
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
But it sends me to http://localhost/xampp/splash.php.
I am still new to .htaccess so I really don't have any idea what I am doing wrong. I've tried the answers that have been given to the questions where the OP said the answer worked.
You can use:
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
RewriteEngine on
RewriteBase /sample/
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
RewriteBase /sample/ is needed to make sure redirect happens from raltive path of current directory.
Use this:
RewriteEngine On
# prevent directory listing
Options -Indexes
# allows leaving off the extension (.php) in urls
Options +MultiViews
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^index(\.php)?$ http://localhost/sample [R=301,L]
I have hide .php extension of all the pages using .htaccess
But when some one types link address with .php extension the page still opens up with .php extension. I have tried permanent redirect .php page to non php page for example example.com/about.php to example.com/about but its giving me redirect loop error.
Here is my .htaccess code
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteRule ^products/(\d+)/([\d\w-\s]*)/([\d\w-\s]*)/([\d\w-\s]*)/?$ product_detail.php?id=$1&category=$3&parent_category=$2 [L,QSA]
</IfModule>
Options +MultiViews
ErrorDocument 404 /404.html
Keep your code like this in root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteRule ^products/(\d+)/([\d\w\s-]*)/([\d\w\s-]*)/([\d\w\s-]*)/?$ product_detail.php?id=$1&category=$3&parent_category=$2 [L,QSA]
You can try adding this to the top of your rules (just under RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(.*)\.php
RewriteRule ^ /%1 [L,R=301]
I use ISPConfig 3 to manage my domains/subdomains.
I added subdomain parim.kristian.ee which would redirect to web/parim/ (note: web/ is my document root).
ISPConfig generated such redirect to apache config:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^parim.kristian.ee [NC]
RewriteRule ^/(.*)$ /parim/$1 [L]
Now if i'll try to get static resources, such as http://parim.kristian.ee/images/1x1.gif, it serves fine, but when redirect is to codeigniter, it doesn't work.
Htaccess in web/parim/ looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
Options -Indexes
#Handle CodeIgniter request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 ./index.php
</IfModule>
NOTE: accessing the same folder via http://kristian.ee/parim/ works!
I s*ck at htaccess, so any help is appreciated.
# To remove index.php from URL
RewriteEngine On
RewriteBase /parim
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Or Simple remove RewriteBase /parim