how to remove parts of a url with htaccess - .htaccess

My url contains a broken link due to the http:// prefix not being added in places. How would I replace this using mod_rewrite:
http://website.com/www.websitelink.com
should go here:
http://www.websitelink.com

RewriteRule ^www\.websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
In other words, if your path is /www.websitelink.com (^ is start of string,$ is end of string; in regular expressions, dots are one-character wildcards and have to be escaped)
(and [NC] matching is not case sensitive - /WwW.webSiteLink.COM would match, too),
[R=301] redirect with status "301 (Moved Permanently)"
to http://www.websitelink.com/
and [L] leave processing (no more rewrite rules are processed).
Note that this will work regardless of the site's domain (would work e.g. for http://website.com/www.websitelink.com and http://www.website.com/www.websitelink.com )
If you want to match all the paths that end with your domain, drop the starting ^:
RewriteRule www\.websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
and if you want to match even paths without www., make it optional:
RewriteRule (www\.)?websitelink\.com$ http://www.websitelink.com/ [R=301,NC,L]
As #Litso noted, this won't match the path after the "domain-in-path"; this should match the trailing path:
RewriteRule (www\.)?websitelink\.com/(.*)$ http://www.websitelink.com/$1 [R=301,NC,L]
To match any subdomain:
RewriteRule ([a-z0-9.-]+\.)?websitelink\.com/(.*)$ http://www.websitelink.com/$1 [R=301,NC,L]
And to match any domain:
RewriteRule ([a-z0-9.-]+\.)?([a-z0-9.-]+)\.com/(.*)$ http://www.$1.com/$2 [R=301,NC,L]

Related

HTACCESS How to "cut" URL at one point

I am new to .htaccess and I don't understand it well. Recently I have built the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /api/v2/
RewriteRule ^api/v2(.*) /api/v2/api.php?input=$1
This was in the root public folder (example.com/.htaccess). But now I have to create second Rewrite and I want to make .htaccess file in example.com/api/v2/ folder. I tried to remove /api/v2/ part in each Rewrite Rule, but only thing I got was error 500.
What I want to achieve:
If someone uses this link: https://example.com/api/v2/test/test/123, I'd like to make it into https://example.com/api/v2/api?input=test/test/123 with .htaccess located in example.com/api/v2 folder.
Addressing your existing rule first:
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /api/v2/
RewriteRule ^api/v2(.*) /api/v2/api.php?input=$1
The first RewriteCond (condition) is entirely superfluous and can simply be removed. The second condition simply asserts that there is a slash after the v2 and this can be merged with the RewritRule pattern. So, the above is equivalent to a single RewriteRule directive as follows:
RewriteRule ^api/v2(/.*) /api/v2/api.php?input=$1 [L]
This would internally rewrite the request from /api/v2/test/test/123 to /api/v2/api.php?input=/test/test/123 - note the slash prefix on the input URL parameter value.
However, unless you have another .htaccess file in a subdirectory that also contains mod_rewrite directives then this will create a rewrite loop (500 error).
Also note that you should probably include the L flag here to prevent the request being further rewritten (if you have other directives).
If someone uses this link: https://example.com/api/v2/test/test/123, I'd like to make it into https://example.com/api/v2/api?input=test/test/123 with .htaccess located in example.com/api/v2 folder.
I assume /api? is a typo and this should be /api.php?. Note also that the slash is omitted from the start of the URL parameter value (different to the rule above).
I tried to remove /api/v2/ part in each Rewrite Rule, but only thing I got was error 500.
This is the right idea, however, you need to be careful of rewrite loops (ie. 500 error response) since the rewritten URL is likely matching the regex you are trying to rewrite.
Try the following instead in the /api/v2/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !api\.php$
RewriteRule (.*) api.php?input=$1 [L]
The preceding RewriteCond directive checks that the request is not already for api.php, thus avoiding a rewrite loop, since the pattern .* will naturally match anything, including api.php itself.
You could avoid the additional condition by making the regex more specific. For example, if the requested URL-path cannot contain a dot then the above RewriteCond and RewriteRule directives can be written as a single directive:
RewriteRule ^([^.]*)$ api.php?input=$1 [L]
The regex [^.]* matches anything except a dot, so avoids matching api.php.
Alternatively, only match the characters that are permitted. For example, lowercase a-z, digits and slashes (which naturally excludes the dot), which covers your test string test/test/123:
RewriteRule ^([a-z0-9/]*)$ api.php?input=$1 [L]
Or, if there should always be 3 path segments, /<letters>/<letters>/<digits>, then be specific:
RewriteRule ^([a-z]+/[a-z]+/\d+)$ api.php?input=$1 [L]

Remove trailing dot in URL domain

We have URLs of the form:
www.dev-studio.co.uk.
www.dev-studio.co.uk./a-sample-image
With the help of .htaccess rules, I am trying to remove the trailing dot (co.uk.) in the end of the domain name but I'm failing.
This is the rule I'm trying:
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.dev-studio.co.uk/%3 [L,R=302,NE]
But the %3 which should capture the 3rd group is returning empty.
The goal is to simple redirect www.dev-studio.co.uk./a-sample-image to www.dev-studio.co.uk/a-sample-image
I have tried all the other questions over here but the solutions are not working for me.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.example.co.uk/%3 [L,R=302,NE]
The HTTP_HOST server variable contains the hostname only (ie. the value of the Host HTTP request header), it does not contain the URL-path, so the %3 backreference is always empty.
You need to either capture the URL-path from the RewriteRule pattern. For example:
RewriteRule (.*) http://www.example.co.uk/$1 [R=302,L]
Or, use the REQUEST_URI server variable (which contains the full URL-path, including slash prefix) instead:
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
This should ultimately be a 301 (permanent) redirect, once you have confirmed it works OK.
Note that since you are redirecting to a specific domain, do you need a CondPattern that matches any .co.uk hostname? You could be specific:
RewriteCond %{HTTP_HOST} =www.example.co.uk.
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
The = prefix on the CondPattern changes it to a lexicographical string comparison (not a regex), so no need to escape the dots.
If you wanted an entirely generic solution to remove the trailing . (FQDN) from any requested host then you could do something like:
RewriteCond %{HTTP_HOST} (.+)\.$
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
Although you might want to combine this with your canonical redirects (eg. non-www to www / HTTP to HTTPS?) to avoid multiple redirects - although they are probably unlikely to occur all at once anyway, so probably not an issue.

Htaccess rewrite with question mark in condition

I want to redirect all
homepage.com/?start=
to
homepage.com
but not all other urls with ?start= such as
homepage.com/xxxx/?start=
i tried this:
RewriteCond %{QUERY_STRING} start [NC]
RewriteRule .* http://homepage.com? [R=301,L]
but sure, it redirect to home page all URLS with ?start
RewriteRule .* http://homepage.com? [R=301,L]
The RewriteRule pattern, (eg. .*) matches against the URL-path.
The regex .* matches everything, so that's why it redirects every URL-path. To match an empty URL-path (ie. just the home page) then restrict the regex to match an empty URL-path. eg. ^$.
RewriteCond %{QUERY_STRING} \bstart= [NC]
RewriteRule ^$ http://example.com/ [QSD,R=302,L]
Assuming you're on Apache 2.4 then use the QSD (Query String Discard) flag instead of appending an empty query string to the substitution string. Note that there should be a slash after the hostname portion of the URL.
Note that the regex start matched that string anywhere (eg. mystart123, starter=, etc.). The regex \bstart= matches just the query string parameter name as stated.
You will need to clear your browser cache before testing.
Test first with 302 (temporary) redirects to avoid caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

htaccess file seems to pick up 'if contains' for a RewriteRule

We currently have a .htaccess RewriteRule that's incorrectly (or correctly as the rule is incorrect) redirecting a URL.
The Rule
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
The desired redirects for this are:
http://domain.com/holiday-ecards/
http://domain.com/holiday-ecards/1/
http://domain.com/holiday-ecards/1/2
http://domain.com/holiday-ecards/1/2/3
However, it seems to also be redirecting the following, which is undesired:
http://domain.com/holiday-ecards-business/
EDIT
/appindex.php
This is taking care of the app routing and works as intended.
A number of ways you could do it, one would be setting a rewrite condition to not touch URI's that have holiday-ecards plus hyphen, like so:
RewriteCond %{REQUEST_URI} !^/holiday-ecards-.*$
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
Not sure how many variations you have of URI's with holiday-ecards in them.
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
(Note that this is an internal rewrite, not a redirect.)
The above RewriteRule pattern makes the slash after holiday-ecards optional (so it will also match holiday-ecards-business). However, in the example URLs that should be rewritten, the slash is mandatory. So, it would appear that you just need to make it mandatory (?), for example:
RewriteRule ^holiday-ecards/ /appindex.php [L]
The trailing pattern .*$ is superfluous.

.htaccess rewriterule /directory/ to php

I have a problem with showing images and css files on my site when using RewriteRule on .htaccess file:
RewriteEngine On
RewriteRule ^home/?$ page.php?id=home [L,QSA]
RewriteRule ^stats/?$ statspage.php?id=abcd [L,QSA]
...but the images and css files not loaded.
Add a RewriteCond before each RewriteRule:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css)$ [NC]
RewriteRule ^home/?$ page.php?id=home [L,QSA]
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css)$ [NC]
RewriteRule ^stats/?$ statspage.php?id=abcd [L,QSA]
RewriteCond provides more detailed qualifications for a particular RewriteRule.
%{REQUEST_URI} is a server variable. It provides the requested URL path, i.e, the portion after the domain name, but not including the query string.
The condition I added to your rules allows all URLs that end in the listed extensions to skip that particular rewrite. Which means Apache can send the resource as it is requested.
The exclamation mark ! means NOT.
Periods have a special meaning in regular expressions so if you want to match an actual period they need to be escaped with a backslash: \..
The combination of parenthesis: () and pipes: | create alternating lists: (gif|jpe?g|png|css). So here any extension will match: gif OR jpeg OR jpg OR png OR css. The question mark after the e in jpe?g makes the e optional, hence jpeg OR jpg.
The dollar sign $ means "end of string", so the matched pattern must be the last thing in the URL path.
In English: Apache mod_rewrite is instructed to match URL paths that do NOT end in a period followed by either "gif", "jpeg", "jpg", "png", or "css". Therefore URLs that do match (images and css) skip the rule.

Resources