Mod-rewrite for GET parameter on subdomains - php

I'm trying to rewrite a subdomain (NOT REDIRECT) to a $_GET parameter as such:
Desired result:
http://go.example.bz/link/abcde -> http://example.bz/go/link?id=abcde
or
http://go.example.bz/hrm/employee/8 -> http://example.bz/go/hrm/employee?id=8
What's currently working:
http://example.bz/go/link/abcde -> http://example.bz/go/link?id=abcde
and
http://example.bz/go/hrm/employee/8 -> http://example.bz/go/hrm/employee?id=8
with this .htaccess in the root:
RewriteEngine On
RewriteRule ^go/link.php/([^/\.]+)/?$ go/link.php?id=$1 [L]
RewriteRule ^go/hrm/employee.php/([^/\.]+)/?$ go/hrm/employee.php?parameter=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
AddCharset UTF-8 .php
Options -Indexes
and this is how I redirect the subdomain:
<VirtualHost *:80>
Servername go.example.bz DocumentRoot /var/www/go
</VirtualHost>
I do not want to redirect to the -> destination, rather to keep the http://go.example.bz/link/abcde url but have the results of the /link?abcde

I figured that my problem came from not remove .php extension from subdomain URLs of which I found an answer here https://css-tricks.com/forums/topic/remove-php-extension-from-subdomain-urls/ and then I alternated my RewriteRules as such:
RewriteEngine On
RewriteRule ^go/link/([^/\.]+)/?$ go/link.php?id=$1 [L]
RewriteRule ^go/hrm/employee/([^/\.]+)/?$ go/hrm/employee.php?id=$1 [L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^go\.example\.bz$ [OR]
RewriteCond %{HTTP_HOST} ^www\.go\.example\.bz$
RewriteRule ^/?$ "http\:\/\/tgc\.bz\/go" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
AddCharset UTF-8 .php
Options -Indexes

You can use this rule in root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^(link)/(.+)$ http://%2/%1/$1?id=$2 [NC,L,QSA,R=302]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^go.example.com
RewriteRule ^(.*?)/(.*)$ http://example.com/go/$1?id=$2 [L]

Does this help:
RewriteCond %{HTTP_HOST} ^go.example.com$
RewriteRule ^link/([a-zA-Z0-9]+)$ http://example.com/go/link?id=$1 [P]

Related

HTTPS and Remove index.php on CodeIgniter

I am using codeigniter framework,set .htaccess file to remove index.php and trying to enable HTTPS protocol for the server, and something happened.
HTTP:
Everything is okay, when I access http://www.example.com/controller/method or http://www.example.com/index.php/controller/method
HTTPS:
Everything is okay, when I access https://www.example.com/index.php/controller/method
Got 404 not found when I access https://www.example.com/controller/method
I think that is .htaccess file problem, it is look like htaccess file not working for HTTPS protocol.
.htaccess file of my site.
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Is something wrong? thanks a lot.
Note: The SSL rewrite should happen before the index.php rewrite in your .htaccess file.
So, Don't do this setting (as we don't change anything in server apache config file.
)
AllowOverride All
just apply the Setting 2 it works.
I have faced the same issue and resolved it by adding
AllowOverride All
Setting 1:
FYI we have 2 config files.
Default Apache config (which runs with http)
SSL config (which runs with https)
Go to SSL config and add
<Directory "/var/www/html">
AllowOverride All
</Directory>
**Setting 2:**The .htaccess file should be like this...
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
It worked for me.
First you remove $config['base_url] from your config file and put ''(blank) in your $config['index'] attribute ..
please set your .htaccess file like..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Try this
I used this in one of my project. May its helps you
#AuthType Basic
#AuthName "restricted area"
#AuthUserFile /home/site_name/public_html/.htpasswd
#require valid-user
RewriteEngine On
#RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?.site_name.lk$ [NC]
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?site_name\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? https://www.%1site_name.com%{REQUEST_URI} [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
# RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Note site_name in the above htaccess should replaced to your website name
you can try this, it works for my project.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
reference: https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
if you want to http->https AND remove www AND remove index.php here's the solution:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>

Redirect url to another

I am using an AWS EC2 instance to host a website. I have an installation under /var/www/html/xyz/ dir & thus if I have to access website homepage, I have to specify link as www.example.come/xyz. I want that www.example.com should be redirected to www.example.com/xyz
Any idea how can I redirect www.example.com to www.example.com/xyz automatically (httpd config or anything similar)?
I checked online but most of the posts are related to subdomain redirects e.g. redirect admin.example.com to www.example.com which is not my issue.
Thanks!
my .htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine on
# Some hostings require RewriteBase to be uncommented
# Example:
# Your url is http://www.yourcompany.com/xyz
# So "RewriteBase" should be:
# RewriteBase /xyz
Options -MultiViews
RewriteBase /xyz
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
</IfModule>
Another option is to change the document root of your site. If you are running Ubuntu, you should have a file in /etc/apache2/sites-available/YOUR_SITE.conf (maybe default.conf). Just edit it and change the DocumentRoot to point to your subfolder.
Create new .htaccess file with following code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /xyz/ [R=301]
you should have mod_rewrite enabled.
Use this code in your htaccess file, this will redirect the user to the subfolder without changing th URL www.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]
Another method with URL www.domain.com/subdir
RewriteEngine On
RewriteRule ^$ /subdir[L]
You can use the following code in Root/.htaccess:
RedirectMatch ^/$ /xyz
This will redirect
http://example.com
to
http://example.com/xyz
And you want to Rewrite http://example.com/foo to http://example.com/xyz/foo , you can use mod_rewrite :
RewriteEngine on
RewriteRule ^/?((?!xyz).*)$ /xyz/$1 [L]
Hope this will help !
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]
or this
Redirect 301 /path/to-old-url http://www.cyourdomain.com/path/to-Try this new-url

Redirect https to http using htaccess in opencart

I have installed opencart 2.0.1.1 and I need to redirect the https://www.domain.com to http://www.domain.com. How do I achieve it using htaccess file.
I have tried this code in htaccess file. But this doesn't work.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How should I redirect the https URL to http. I also checked config.php file in opencart it doesn't contain any https URL.
The existing htaccess file of opencart contains the following code
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
But the file is not a htaccess file. Its only a text document named as htaccess.txt.
Thanks in advance.
The last one ;)
You can try to put this code, change for your domain
In the files config.php and admin/config.php change the reference to https, replace "http".
define('HTTPS_SERVER', 'http S://www.example.com/');
define('HTTPS_IMAGE', 'http S://www.example.com/image/');
replace
define('HTTPS_SERVER', 'http://www.example.com/');
define('HTTPS_IMAGE', 'http://www.example.com/image/');
My htaccess file:
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourDomain.com
RewriteRule ^ http://yourDomain.com%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
regards,
This is the solution:
RewriteCond %{HTTP_HOST} ^yourDomain.com
RewriteRule ^ http://www.yourDomain.com%{REQUEST_URI} [L,R=301]
Or , if your prefer don't use www ,
RewriteCond %{HTTP_HOST} ^yourDomain.com
RewriteRule ^ http://yourDomain.com%{REQUEST_URI} [L,R=301]
And, in the admin panel:
system->settings->server-> ssl= NO
Regards,
yes, ok
I thought that you had a "correct" htaccess. Please make the following:
Create a new file .htaccess or rename the htaccess.txt to .htaccess . The "dot" in the name of the file is important. ".htaccess" (I recomend you rename the file)
put that file first
RewriteEngine On
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourDomain.com
RewriteRule ^ http://www.yourDomain.com%{REQUEST_URI} [L,R=301]
Or , if your prefer don't use www ,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourDomain.com
RewriteRule ^ http://yourDomain.com%{REQUEST_URI} [L,R=301]
And, in the admin panel:
system->settings->server-> ssl= NO
system->settings->server-> Url SEO -> YES
Please, try again :)
regards,
Add this code to the .htaccess file, it should redirect to what you have in the config files.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

Redirect language query string to subdirectory for better SEO

I'm developing a web site that supports multiple languages (english and spanish) in PHP using parameters like this:
www.website.com/contact?lang=en
www.website.com/contact?lang=es
Yes, without extension .php...
I know that is a bad practice, so I want to have this:
www.website.com/en/contact
www.website.com/es/contact
I read other posts here in stackoverflow but, I don't want to use a PHP framework, and I read that I have to use Mod Rewrite (again because I already use it for remove the extension). In fact, I read this post (and others too), but nothing works.
When it is done, I read in google pages that I have to use:
<link rel="alternate" href="http://www.website.com/" hreflang="x-default" />
is this correct?
Thanks
EDIT 1:
The content of my .htaccess is:
RewriteBase /
#Prevent directory listing
Options All -Indexes
#Disable Etags
Header unset ETag
FileETag None
#Prevent viewing of .htaccess
<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
</Files>
RewriteEngine on
#Redirect non-www/www
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Options +FollowSymlinks -MultiViews
#Rewrite url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^/]+) /$2?lang=$1 [L,NC,QSA]
even if you use the solution from the post you marked out with a tiny adaption to meet your missing php file endings:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^/]+) /$2?lang=$1 [L,NC,QSA]
This should be your full .htaccess with a new rule to redirect lang specific URLs:
#Disable Etags
Header unset ETag
FileETag None
#Prevent viewing of .htaccess
<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
</Files>
Options All -Indexes -MultiViews
RewriteEngine on
RewriteBase /
#Redirect non-www/www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect /contact?lang=en to /en/contact
RewriteCond %{THE_REQUEST} /+([^?]+?)(?:\.php)?\?lang=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([^/]+)/([^/]+) $2?lang=$1 [L,QSA]
This should take care of the redirects. You'll need to modify your links as #w3d has stated.
RewriteCond %{QUERY_STRING} ^lang=(en|es)$ [NC]
RewriteRule ^contact$ /%1/contact? [R=301,L]
The | in the query string means 'or' if you want other languages you can add them in there or you could change it to the less safe .*.

Load another domain images to my domain using htaccess?

I have 2 domains and 2 servers example.com and example2.com. for eg: example.com is my main domain. i upload my site images into example2.com
my current image path http://example.com/data/photos/sample.jpg.
but my all images are in http://example2.com/data/photos/sample.jpg
So i create htaccess 301 redirect
RewriteEngine on
RewriteBase /
RewriteRule ^data/photos/(.*)$ http://example2.com/data/photos/$1 [R=301]
But its not working for me. Because of i called full URL into my all pages. so any one suggestion its very helpful for me
Updated my old Htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
RewriteEngine On
RewriteBase /
RewriteRule ^data/photos/(.*)$ http://example2/data/photos/$1 [R=301]
</IfModule>
You need to reorder your rules. Your final htaccess should look something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(data/photos/.*)$ http://example2.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
change it with this
RedirectMatch 301 ^/data/photo/ http://example2.com/data/photo/
to create a 301 for the entire directory.

Categories