I can't figure it out at all, does anyone know how to describe a link with .htaccess of:
example.com/bedrijven/marketing-metrics/
to
marketing-metrics.example.com/
I have found this on this forum but it wasn't the solution
RewriteRule ^(bedrijven/$1.*)$ https:///$1.example.com/ [R=301,L,NC]
is there anybody who can help?
Try something like the following instead:
RewriteRule ^bedrijven/([^/]+)/$ https://$1.example.com/ [R=301,L,NC]
The $1 is a backreference to the first capturing group in the RewriteRule pattern (first argument), ie. whatever is matched by ([^/]+). It makes no sense to use a backreference like this in the RewriteRule pattern itself, as you used in your example.
Related
How can modrewrite be done in this above situation:
Lets say the website us is: www.real-estate.com
Then we have the first mod rewrite:
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So this will rewrite to something similar: www.real-estate.com/florida and it will list all the real estates in florida.
Then we add this rule:
RewriteRule ^([a-zA-Z\-]+)/(.*)+$ details.php?project=$2
This will rewrite to www.real-estates/florida/project-one and it will display details from that project.
But if I access the link for the city like this: www.real-estae.com/florida/ (with slash last) it will jump me to the second mod rewrite rule, taking me to details.php with an empty variable.
What is the correct solution to slove this problem, so that if the users adds a slash after the city it will still display the projects in that city and not the details page with empty var ?
After playing around I found that his works, but I do not know if it is the correct solution:
I replaced this:
RewriteRule ^([a-zA-Z\-]+)/+(.*)$ details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
With this:
RewriteRule ^([a-zA-Z\-]+)/([a-zA-Z\-]+) details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So I have replaced (.*)$ Why does this work and the other way not I do not know.. Maybe someone can explain.
Probably it fixed the immediate problem but your rules might still match more than required due to faulty regex used and not using anchor $.
For your case use these rules:
RewriteEngine On
RewriteBase /
RewriteRule ^[a-zA-Z-]+/([a-zA-Z-]+)/?$ details.php?project=$1 [L,QSA]
RewriteRule ^([a-zA-Z-]+)/? cities.php?city_url=$1 [L,QSA]
Apache mod_rewrite Introduction
I'm using mod_rewrite for rewriting my links as follows. I defined a redirect from /test/1234_5678_... to /test.php?id=1234 as follows:
RewriteRule test/(.*)_(.*)$ test.php?id=$1
It works perfectely. Now I wanted to add the following redirect: /test/1234_5678_.../print to /test.php?id=1234&print. Therefore I added the following line before the one above. The redirect is not working and it seems as if only the second rule applies. Am I doing anything wrong with the pattern matching? Is it a problem that there can be more than one underscore and I only used one in the pattern?
RewriteRule test/(.*)_(.*)/print$ test.php?id=$1&print
RewriteRule test/(.*)_(.*)$ test.php?id=$1
Both rules work fine for me, but you probably want to change the first grouping to ([0-9]+) or ([^_]+), and the second group to [^/]+, and add some L flags:
RewriteRule test/([^_]+)_([^/]+)/print$ test.php?id=$1&print [L]
RewriteRule test/([^_]+)_([^/]+)$ test.php?id=$1 [L]
When moving all pages from old domain to new domain, i noticed some people added a ^and other not in rewriterule
What is the different between
RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301,L]
and
RewriteRule (.*)$ http://mynewdomain.com/$1 [R=301,L]
thank you.
They are both wrong. There is no need to match anything in a backreferennce because Apache has a built in variable for the current URL: %{REQUEST_URI}.
RewriteRule .? http://www.newdomain.com%{REQUEST_URI} [L,R=301]
As for your question, the meaning of ^ is 'matches at the start of the URLand$` is 'matches at the end of the URL'. This is probably easiest by example.
^welcome/ matches the url /welcome/a/b/c, /welcome/b/c/d, etc, anything that starts with /welcome
welcome$ matches /a/b/welcome, /a/something/welcome, etc, anything that ends with a 'welcome'
The first RewriteRules you present in your question is 'An URL that has a start and an end and something inbetween', while the second one is 'a URL that has some text and then ends'. Both expressions are very generic and match anything you throw at it.
It is always better to use start and end anchors (^ and $) wherever possible like ^(.*)$ but in the 2 cases you listed both rules will behave similar.
Is it possible to edit htacces in such a way that only the following url is rewritten and the rest isn't?
http://www.example.com/index.php?x=foobar
to
http://www.example.com/foobar/
I want the pages not having x=... as a variable to behave normally
I got the following but that doesn't work
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/ index.php?x=$1
RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav|txt)$
Who can help me?
First off, the RewriteCond must be put before the RewriteRule to which it belongs.
But I think that you need another approach for your case, something like this:
RewriteRule (.*)\.php - [PT,L]
RewriteRule ^(.*)/$ index.php?x=$1
The first rule Passes Through (PT) every PHP page, so the second rule is only applied to all non-PHP requests.
That second rule only applies to a "simple path", no matter if this path has a dot in it or not (e.g. hello.gif/ will match, too).
If this does not work for you, then you might consider one of these points to start further research:
the pattern ([^\.]*) matches everything that does not have a dot in it
see RewriteCond to skip rule if file or directory exists for RewriteConds where the following RewriteRule is only used if the request does not point to an existing file or directory
Hope this helps.
What is wrong with this rewrite rule?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [L]
I simply want "index.php?url=" to be added after api/ and before the rest of the get parameters.
api/image/upload&arg1=1&text=lorem+ipsum
to
api/index.php?url=image/upload&arg1=1&text=lorem+ipsum
What is wrong with (.+) to get everything after api/?
The regex on the RewriteRule is only run against the path part of the URL, not the query parameters. Fortunately there is the [QSA] flag to preserve existing query parameters.
Are you doing something to stop infinite recursion?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [R=301,L]
or some equivalent
I think you must write your domain name before the regex stuff. Like this:
RewriteRule ^(.+).com/api/(.*)$ "$1.com/api/index.php?url=$2" [R=301,L]