How to use apache's mod_rewrite rewriterule without changing relative paths - apache

I've the following rewrite rule in .htaccess:
RewriteRule ^groups/([^/\.]+)/?$ groupdetail.php?gname=$1 [L,NC]
This takes something like www.example.com/groups/groupname and calls www.example/groupdetail.php?gname=groupname. And it works just fine.
But all the relative links on groupdetail.php use groups/ as the relative path, and I don't want them to. How do I avoid this?
For example, when a user clicks on a link <a href="link.php"> on groupdetail.php?gname=groupname, he's taken to www.example/groups/link.php. I want to take the user to www.example.com/link.php.
Obviously, I want to URL to the user to look like "www.example.com/groups/groupname" so I don't want to use [R]/redirect.

If like me you had hundreds of relative links in the page, insert a <base href=""> in the <head> with an absolute path (could use relative too). You'll need to also make the path to .js files in the <head> absolute because IE and firefox deal with the base href differently. I agree it is an annoying issue.

Relative links are resolved by the browser, not the server, so there is nothing you can do with mod_rewrite.
Either use relative links the go up the hierarchy (../link.php) or use absolute links.

If you do not want to have absolute links or use <base> because you are going to move the page around, you can have the base be generated by php, as following:
echo '<base href="http://'.$_SERVER['SERVER_NAME'].str_replace("index.php","",$_SERVER['PHP_SELF']).'" />';

If you change the rewite rule to do a force redirect (add the [R] option), then the browser will be using the /groupdetail.php URL and the relative links will work fine. However, that adds one redirect and makes the URLs less pretty.
RewriteRule ^groups/([^/.]+)/?$ groupdetail.php?gname=$1 [L,NC,R]

You can use the BASE tag, if you don't want to use absolute paths:
http://www.w3schools.com/tags/tag_base.asp

Hop's answer is correct. The browser sees www.example.com/groups/groupname as the address, so considers that /groups is the current directory. So, any links like <a href=link.php> are assumed to be in the /groups folder.
When the user moves his mouse over the link, he'll see www.example.com/groups/link.php as the link address.
The solution is to use absolute links -- just add a slash before the href:
<a href=/link.php>
The user will then see www.example.com/link.php as the url.
That said, it seems from your question that you are using relative links on purpose... do you have a reason not to use absolute links?

Related

.htaccess redirect based on a part of URL

After site crash a redirect php script doesn't work as expected.
We try to fix it, but in the meantime we are looking for a quick solution to redirect search engine results so our visitors can at least visit after clicking a relative web page.
The url structure or the search engines result are something like this:
https://www.example.com/MainCategory/SubCategory_1/SubCategory_2/Product?page=1
and I'd like to redirect using the "SubCategory_2" part of the URL to something like this
https://www.example.com/SubCategory_2.php
so until we fully repair the script at least our visitors will se a relative web page.
I'm quite stuck... Any ideas?
Thank you
To redirect the stated URL, where all parts are variable (including an entirely variable, but present query string) then you can do something like the following using mod_rewrite near the top of your root .htaccess file (or crucially, before any existing internal rewrites):
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^[^/]+/[^/]+/([^/]+)/[^/]+$ /$1.php [QSD,R=302,L]
The QSD flag is necessary to discard the original query string from the redirected response.
The above will redirect:
/MainCategory/SubCategory_1/SubCategory_2/Product?page=1 to /SubCategory_2.php
/foo/bar/baz/qux?something to /baz.php
You can test it here using this htaccess tester.
UPDATE:
unfortunately without success. I get 404 error.
You'll get a 404 if the directive did not match the requested URL, or /SubCategory_2.php does not exist.
Is the URL redirected? What do you see in the browser's address bar?
If there was no redirect then the above rule did not match the requested URL and the rule did nothing. Either because:
The URL format is not as stated in the question.
The rule is in the wrong place in the .htaccess file. As stated, this rule needs to be near the top of the config file.
I found a basic solution here htaccess redirect if URL contains a certain string I crate something like this RewriteRule ^(.*)SubCategory_2(.*)$ https://example.com/SubCategory_2.php[L,R=301] and works just fine. My problem is that this is a "static solution" since "SubCategory_2" is a variable.
Ok, but that is a very generic (arguably "too generic") solution for the problem you appear to be attempting to solve. This matches "SubCategory_2" anywhere in the URL-path (not just whole path segments) and preserves any query string (present on your example URL) through the redirect. So, this would not perform the stated redirect on the example URL in your question.
However, the directive you've posted (which you say "works just fine") cannot possibly work as written, at least not by itself. Ignoring the missing space (a typo I assume) before the flags argument, this would result in an endless redirect loop, since the target URL /SubCategory_2.php also matches the regex ^(.*)SubCategory_2(.*)$.
Also, should this be a 301 (permanent) redirect? You seem to imply this is a "temporary" solution?
HOWEVER, it's not technically possible to make "SubCategory_2" entirely variable in this "basic solution" and search for this variable "something" in a larger string and redirect to "something.php". How do you know that you have found the correct part of a much larger URL? You need to be more specific about what you are searching for.
In your original question you are extracting the 3rd path segment in a URL-path that consists of 4 path segments and a query string. That is a perfectly reasonable pattern, but you can't extract "something" when you don't know what or where "something" is.

Modern favicons & htaccess rewrite rule

Instead of having all the new favicon formats placed into the root directory of my website, I am placing them inside a subfolder.
To conform to the standards, as some browsers / device versions do not use the path as directed inside the html meta tags, but instead try to get the file from the website root anyway, I am creating a rewrite rule to redirect all these files to the actual location - but ONLY these files.
What I have come up with so far is the following :
RewriteEngine On
RewriteRule ^/((apple\-touch\-icon|android\-chrome|favicon|mstile)-([0-9]+)x([0-9]+).png|manifest\.json|browserconfig\.xml|favicon\.ico|(apple\-touch\-icon\-precomposed|apple\-touch\-icon).png|safari-pinned-tab.svg)$ /favicon/$1 [L]
This should match all of the following files :
When testing the rule at this site, the rule is not matched (see pic):
I would like to keep this rule on the same line, and due to the size standards changing, I wish to keep this dynamic (aka, instead of specifying each individual file, use a mask as I attempted to do). I suspect something with my regex is off.
Please assist or provide a solution with the corrected regex pattern for what I am intending to achieve.
Modern favicons + Rewrite
Following is a fairly robust pattern for mapping the modern favicon's using rewrite.
Regex pattern, for reference
^(browserconfig.xml|manifest.json|safari-pinned-tab.svg|(android-chrome|favicon|mstile)-[0-9]+x[0-9]+.png|apple-touch-icon(-precompressed.png|-[0-9]+x[0-9]+.png|.png)|manifest.json)$
Usage: apply it to a rewrite rule (htaccess)
This example assumes the rewrite destination where the favicon's are placed is a folder named favicon (or whatever folder you wish).
RewriteRule ^(browserconfig.xml|manifest.json|safari-pinned-tab.svg|(android-chrome|favicon|mstile)-[0-9]+x[0-9]+.png|apple-touch-icon(-precompressed.png|-[0-9]+x[0-9]+.png|.png)|manifest.json)$ /favicon/$1 [L]
I don't think this will work.
For one thing, its "precomposed" not "precompressed"
It looks like you're evaluating 'manifest.json' twice
Also, I don't know of any icons that show single-digit sizes; they
would be really tiny, e.g., apple-touch-icon-precomposed-1x1. They
are all 2 or 3 digits, e.g.,
apple-touch-icon-114x114-precomposed.png or favicon-16x16.ico
Cheers!

htaccess rule - relative files

It's probably an easy thing to fix however I tried to google this stuff but I'm not sure how to put it in words so I couldn't find anything which would help me.
I have a problem where my very simple .htaccess changes my url as it supposed to but all the resources are trying to get loaded from a wrong place.
My Url:
http://domain.com/index.html?sport=test
My re-written Url:
http://domain.com/test/
.htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.html?sport=$1 [L]
now when I type in: http://domain.com/test/ it loads the correct index file however every resource file is trying to get downloaded from test folder...
this is an example resource file location (relative to the index.html):
css/styles.css
js/main.js
but it's looking for them in:
test/css/styles.css
test/js/main.js
Cheers
You've hit the most common problem people face when switching to pretty URL schemes. Solution is also simple, just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
OR you can try adding this in your page's HTML header:
<base href="/" />

htaccess different behaviour with trailing slash on the URL

I have the following rules in .htaccess file placed at the root folder
RewriteEngine on
RewriteRule ^/?$ /testsite/index.php?rel=m
RewriteRule ^about/?$ /testsite/aboutus.php?rel=b
RewriteRule ^ict/?$ /testsite/ict.php?rel=b
RewriteRule ^eeap/?$ /testsite/eeap.php?rel=b
The rule for index works fine with or without a trailing slash. However when I hit http://mydomain.com/testsite/about/ the page is served without CSS and images and works fine if the last slash is removed. This happens with the rule for ict too.
For the next rule (eeap) things behave the other way. CSS and images go missing if I end the URL with a slash.
What am I doing wrong here?
You need to think about this from the point of view of the web browser.
If the URL does not end with a slash, "/about" for example, then the web browser assumes that it is looking at a file called "about" in the top-level directory of the web server. So any requests in the page generated by "/about" which have relative paths will cause the web browser to produce a full path based on starting off in the top-level directory. For example, a CSS file path of "css/style.css" will cause the browser to start from the top-level and ask for the file from "/css/style.css".
But if the URL does end with a slash, "/about/" in our example, then the web browser believes that it is looking at a sub-directory called "about". Now that same CSS file path "css/style.css" will cause the web browser to ask for the file from "/about/css/style.css".
One CSS file path will be correct, the other will not. Hence the behaviour you're seeing.
There are a few ways to avoid this. You can tell Apache to permanently redirect all requests to either the URL which ends with a slash, or the URL which does not, so that all visitors will end up looking at the same URL. This makes it possible to know what relative CSS and image paths to use in your web pages.
Or you can modify your web pages so that all file paths for CSS, image, JavaScript, etc, are anchored to the top-level directory. So "css/style.css" would become "/css/style.css" for example (note the forward-slash at the start of the path to tell the browser to start from the top-level directory).
Or you could add a bunch of convoluted mod_rewrite rules to try to catch all possible outcomes for CSS files and the like. I don't favour this approach because it will usually make your ruleset more fragile and complicated.

Htaccess rewrite rules subfolder

i'm quite new in the world of Url Rewriting. I have to do so into our own CMS to have a good looking url and to get a better structure that gives results with search engine (SEO).
This is the background file that do all the job that we now hide with nice URL's:
http://something.com/fr/index.php
This is an example of my structure pattern :
http://something.com/fr/accueil or ...
http://something.com/fr/produits/solives-de-rive-rimboard
The CMS simply create an HTACCESS file into the FR directory for quite basic rewriterule like the following :
Options +FollowSymlinks
RewriteEngine On
RewriteRule produits/solives-de-rive-rimboard index.php?p=144
RewriteRule produits/decoupe-sur-mesure index.php?p=145
RewriteRule produits/panneaux-osb index.php?p=146
RewriteRule produits/boites-de-bois-crates index.php?p=147
RewriteRule produits/palettes-de-bois index.php?p=148
RewriteRule accueil index.php?p=4
RewriteRule a-propos-de-cimdat index.php?p=139
RewriteRule produits index.php?p=140
RewriteRule videos index.php?p=141
RewriteRule nous-joindre index.php?p=142
All works fine for first level page like "accueil", "nous-joindre"...
but I haven't found the work around for page of second and third level like "produits/solives-de-rive-rimboard". The index.php just load fine except that all relative link to this page are a subfolder away (the css, the image, jquery...).
Is there any tag that I can use into the htaccess file to specify that no matter in which level "accueil", "produits/something", or a third level one like "produits/group/something" for the index.php have a basic path ?
I'll prefer looking a work around within the htaccess instead of giving index.php a "<base href="something">" that might give us other problems in our structure.
Thanks
This is an HTML issue, rather than a server or .htaccess problem. If your browser sees something like /category/product it is going to act as if it is in the /category/ folder regardless of where the internal server side redirects are sending the request.
The fix to this is simple, change all of the linking in your html to be relative to the site root. So if you have an image tag like
<img src="img/button.gif" />
change it to
<img src="/img/button.gif" />
This tells the browser the exact path from the root to request files from regardless of your rewrite rules. This needs to be done for all of your relative links, including css and javascripts. It may be a bit of work to do, but once it is done it should make future maintenance much easier since you won't have to worry about the apparent path to the page.
Yes, this could be done via .htaccess fairly simply, but there are side effects, including the fact that search engines could see it as duplicate content and it could increase your server bandwidth use as users can't cache static content efficiently. Your best bet is to follow the best practice now while the site is new and growing than try to fix it later after a workaround has caused problems.