RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/articles/([0-9]+)/$ /article-one.html?aid=$1 [L]
from SEO friendly URL eg.
https://example.com/articles/99/aaaa-aabbbb-bb-cccccccc
rewrite to
https://example.com/article-one.html?aid=99
why does it not work?
^/articles/([0-9]+)/$
In a directory context (ie. .htaccess) the URL-path matched by the RewriteRule pattern does not start with a slash. However, this would only match URLs of the form /articles/99/, not /articles/99/aaaa-aabbbb-bb-cccccccc - as stated in your example.
You would need to modify the RewriteRule pattern to something like the following instead:
^articles/([0-9]+)/[\w-]*$
This specifically matches an optional trailing slug of the form aaaa-aabbbb-bb-cccccccc (no trailing slash). If you simply wanted to ignore anything that comes after the numeric path segment then just remove the trailing $ (end-of-string anchor). For example:
^articles/([0-9]+)/
Related
Last week I have posted something similar like the question whereas I wanted to know how to remove trailing slash using .htaccess for a particular page and rest pages will be redirect with trailing slash.
abc.com/demo/ should redirect to abc.com/demo
And I got the solution as following.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/demo/ [NC]
RewriteRule ^demo/$ /demo [L,R]
RewriteCond %{REQUEST_URI} !^/demo [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
But now I need for multiple urls. eg. demo2, demo3, demo4 etc, so in those scenario how will be the code, need suggestion.
abc.com/demo2/ redirect to abc.com/demo2
abc.com/demo3/ redirect to abc.com/demo3
abc.com/demo4/ redirect to abc.com/demo4
To remove traling slash from multiple uris ,you could a regular expression capture-group that matches more then one values at one time something like the following :
RewriteCond %{REQUEST_URI} ^/(demo1|demo2|demo3|demo4)/$ [NC]
RewriteRule /$ /%1 [L,R]
The %1 in the above rule is a RewriteCond backreference and it holds the value matched inside
(demo1|demo2|......) regex pattern ie: demo1 .
You can use the following htaccess to remove and add traling slashes .
RewriteEngine on
# remove traling slash from spacifc uris
RewriteCond %{REQUEST_URI} ^/(demo1|demo2|demo3|demo4)/$ [NC]
RewriteRule /$ /%1 [L,R]
# add traling slash to other uris except the spacifc ones
RewriteCond %{REQUEST_URI} !^/(demo1|demo2|demo3|demo4)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Make sure to clear your browser cache before testing this.
How to combine the following rules ?
the rule to redirect whole domain, and the rule to capture controller/action to $_GET ?
according http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file , but not working for me
RewriteCond %{HTTP_HOST} ^somedomain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.somedomain.com/$1 [R]
or according
Redirect to HTTPS with .htaccess with a specific domain , but not working for me
RewriteCond %{HTTP_HOST} ^somedomain\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
and the rule to capture controller/action to $_GET['url']
RewriteRule ^index.php?(.+)$ index.php?url=$1 [QSA,L]
Working redirect, with action/controller capturing to $_GET['url']
RewriteEngine on
IndexIgnore *
RewriteBase /public
#redirects somedomain.biz to https://www.somedom.biz
RewriteCond %{HTTP_HOST} ^somedomain\.biz [NC]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI}
#redirects www.somedomain.biz to https://www.somedomain.biz
RewriteCond %{HTTP_HOST} ^www.somedomain\.biz [NC]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
#captures controler/action?parameters to $_GET['url']
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{ENV:REQUEST_FILENAME} !-d
RewriteCond %{ENV:REQUEST_FILENAME} !-f
RewriteCond %{ENV:REQUEST_FILENAME} !-l
RewriteRule ^index.php?(.+)$ index.php?url=$1 [QSA,L]
Flags - http://httpd.apache.org/docs/current/rewrite/flags.html
L means last
R - redirect
QSL - By default, the first (left-most) question mark in the substitution delimits the path from the query string. Using the [QSL] flag instructs RewriteRule to instead split the two components using the last (right-most) question mark. This is useful when mapping to files that have literal question marks in their filename.
NC|nocase - Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.
I have this line here;
RewriteRule ^news/(.*)/(.*)/$ ./news.php?type=$1&number=$2 [L]
But when one of the 2 values is empty it shows an error that the page is not found. As example I did;
localhost/news/dgfgh
Is there a way to fix this?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} "/admin/"
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteRule ^news/(.*)/(.*)/$ ./news.php?type=$1&number=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
The Regular Expression in your rewrite rule does not match localhost/news/dgfgh
The rewrite rule is looking for news followed by exactly two groups, followed by a trailing slash. To do what you want, you need two rules.
RewriteRule ^news/(.*)/(.*)/?$ ./news.php?type=$1&number=$2 [L]
RewriteRule ^news/(.*)/?$ ./news.php?type=$1 [L]
The first one is yours with a simple ? before the trailing slash to indicate that the trailing slash is options. The second one is for the case when you don't have the number in the url as in your example
My goal is to point:
http://xyz.domain.com/abc to http://www.domain.com/dir/file.php?var=xyz
(In other words, anything with http://_____.domain.com/abc would read from http://www.domain.com/dir/file.php?var=_____.)
I used this in my .htaccess file at the root of the website (http://www.domain.com/.htaccess):
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^abc/*$ dir/file.php?var=%2 [NC,L]
It worked, but then it automatically redirected to http://xyz.domain.com/abc/?var=xyz when I just wanted http://xyz.domain.com/abc without the query string showing up.
I considered adding a %{QUERY_STRING} line to the above but got stuck when trying to match the two RewriteCond's vars to each other.
Then I found this at http://www.askapache.com/htaccess/modrewrite-tips-tricks.html#Removing_Query_String
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.askapache.com%{REQUEST_URI}? [R=301,L]
...which I'm guessing it's the right code to get rid of something like this on a regular domain? I tried making it suitable for wildcard subdomains by changing it to this:
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com[NC]
RewriteRule .* http://%2.domain.com%{REQUEST_URI}? [R=301,L]
But it doesn't work. SO...can anyone help?
There's a Module called mod_dir that redirects the browser when it thinks you are trying to access a directory but are missing the trailing slash. When I test on my apache it only happens when the directory actually exists, I get redirected with a trailing slash, but the rewrite happens and I see the query string. Try forcing, internally, the trailing slash and when rewriting to dir/file, expressly use a trailing slash:
# Force trailing slash when accessing /abc without one
RewriteRule ^abc$ /abc/ [NC,L]
# these are the same
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
# Match against a trailing slash
RewriteRule ^abc/$ dir/file.php?var=%2 [NC,L]
I'm trying to force a trailing slash to my URLs, but I can't make it work the way I want. This is my .htaccess file:
RewriteEngine on
#Force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
#Subdomains
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond $1/%1 !^([^/]+)/\1$
RewriteRule ^/([^/]+)? /%1%{REQUEST_URI} [L]
#Point everything to page.php
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond $1 !^(.*).(php|css|js|png|jpg|gif|htm|html)$
RewriteRule ^(.*)$ page.php?q=$1 [L,NC]
If I go to "en.example.com/about" I'm redirected to "en.example.com/en/about/", which is an invalid page.
How can I make this work?
The problem here is that the L flag causes a restart of the rewriting process with the rewritten URL (I’ve already told you that, didn’t I?):
Remember, however, that if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.
Now when /about is requested, the first rule get’s applied and redirects to /about/. The subsequent request of /about/ is then processed, at first the third rule is applied and the URL path is rewritten to /page.php. So far, so good.
But now the internal redirect takes place and the rewriting process is restarted with the new URL path /page.php. This is then fetched by the first rule again and redirected externally to /page.php/.
The second rule shouldn’t be applied at all as the pattern ^/ should never match as the per-directory path prefix is removed before testing the pattern when using mod_rewrite in an .htaccess file:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
But these rules should work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteCond $0/%1 !^([^/]+)/\1$
RewriteRule ^[^/]* /%1%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} !=www.example.com [NC]
RewriteCond $1 !.*\.(php|css|js|png|jpg|gif|htm|html)$
RewriteRule .* page.php?q=$0 [L]